Hi Experts,
The code below generates Customer ID automatically:
Private Sub autogenerated()
Dim connStr As String = ConfigurationManager.ConnectionStrings("SY_InventoryConnectionString").ConnectionString
Dim con As New SqlConnection(connStr)
con.Open()
Try
Dim cmd As New SqlCommand("SELECT COUNT(CustID) as Tot FROM Customer", con)
Dim dr As SqlDataReader
dr = cmd.ExecuteReader()
While dr.Read()
Dim i As Integer = Convert.ToInt32(dr("tot"))
If i > 0 Then
Dim j As Integer = i + 1
txtcustid.Text = "C0" + j.ToString()
Else
txtcustid.Text = "C01"
End If
End While
con.Close()
Catch ex As Exception
End Try
End Sub
The problem is, when i delete a row that is before the last inserted row i get the following error:
Violation of PRIMARY KEY constraint 'PK_Customer'. Cannot insert duplicate key in object 'dbo.Customer'.
The statement has been terminated.
Note: Customer ID data type is Varchar(50)
Thanks in advance.