Short background: I'm making a booking system which once it has the booking form filled out will allow the user to print it out before it's stored. For the information to be accurate, the Tracking Number must be displayed when "New record" is clicked. This is the primary key which auto increments in a table (Repair). Currently I'm trying to pull through that unique value when "New Record" is clicked to the "tracking number" textbox. To do this i find the last record and add 1.
When i click the "New record" button a 0 pops up in the tracking number textbox instead of the last record's number (+1) which should be 1002 as the last record in the table is 1001.
Below is my code:
Protected Sub btnNewRepair_Click(sender As Object, e As EventArgs) Handles btnNewRepair.Click
Dim NewTrackingNum As Integer
Dim con As SqlConnection = New SqlConnection("Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\ITrepair.mdf;Integrated Security=True")
Dim cmd As SqlCommand = New SqlCommand("SELECT MAX(Tracking_Number) +1 As NewTrackingNum FROM Repair")
sqlConn.Open()
sqlCmd.ExecuteNonQuery()
sqlConn.Close()
txtTrackingNumber.Text = NewTrackingNum
End Sub
Note: Only one user will be able to use this system at a time which will prevent the errors of more than one user getting the same Tracking Number