Today, We will discuss about "How to Auto Increment in Vb.Net?"
In this Code, I create customer Id like "CI00001"
Sub CustomerId()
Dim strID As String = ""
Dim St As String = ""
CON = ConOpen()
CMD = New SqlCommand("pass your sp or directquery", CON)
CMD.CommandType = CommandType.StoredProcedure
CON.Open()
DR = CMD.ExecuteReader
If DR.Read() Then
strID = DR(0).ToString
End If
DR.Close()
CON.Close()
Dim INNO As Integer
Dim IndID As String = ""
Dim Num As Integer = "0001"
Dim CPCode1 As String()
Dim CPComp As String
If strID = "" Then
txtCustomerId.Text = "CI0000" & Num
Else
CPCode1 = strID.Split(New Char() {"I"c})
For Each CPComp In CPCode1
Console.WriteLine(CPComp)
IndID = CPComp
Next
INNO = IndID + Num
If INNO < 10 Then
txtCustomerId.Text = "CI0000" & INNO
ElseIf INNO >= 10 And INNO <= 99 Then
txtCustomerId.Text = "CI000" & INNO
ElseIf INNO >= 100 And INNO <= 999 Then
txtCustomerId.Text = "CI00" & INNO
ElseIf INNO >= 1000 And INNO <= 9999 Then
txtCustomerId.Text = "CI0" & INNO
ElseIf INNO >= 10000 Then
txtCustomerId.Text = "CI" & INNO
End If
End If
End Sub
Dim strID As String = ""
Dim St As String = ""
CON = ConOpen()
CMD = New SqlCommand("pass your sp or directquery
strID = DR(0).ToString
End If
DR.Close()
CON.Close()
Dim INNO As Integer
Dim IndID As String = ""
Dim Num As Integer = "0001"
Dim CPCode1 As String()
Dim CPComp As String
If strID = "" Then
txtCustomerId.Text = "CI0000" & Num
Else
CPCode1 = strID.Split(New Char() {"I"c})
For Each CPComp In CPCode1
Console.WriteLine(CPComp)
IndID = CPComp
Next
INNO = IndID + Num
If INNO < 10 Then
txtCustomerId.Text = "CI0000" & INNO
ElseIf INNO >= 10 And INNO <= 99 Then
txtCustomerId.Text = "CI000" & INNO
ElseIf INNO >= 100 And INNO <= 999 Then
txtCustomerId.Text = "CI00" & INNO
ElseIf INNO >= 1000 And INNO <= 9999 Then
txtCustomerId.Text = "CI0" & INNO
ElseIf INNO >= 10000 Then
txtCustomerId.Text = "CI" & INNO
End If
End If
End Sub
In this Code, I create customer Id like "CI00001"