Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to display auto increment number from sql server to vb.net windows form to text box/lsbel

What I have tried:

Dim Nextid As String
       Dim connection As New SqlConnection("server= madhuresh-pc; database= madhu; integrated security= true")
       connection.Open()

       Dim cmd As New SqlCommand("SELECT MAX(caseno+1) As 'NextId' FROM case1", connection)
       Nextid = cmd.ExecuteScalar()
       txtstation.Text = Nextid
       connection.Close()
   End Sub
Posted
Updated 20-Sep-17 5:40am
Comments
ZurdoDev 20-Sep-17 11:17am    
This code looks OK. What's the problem?

1 solution

NO.
Do not do it, ever.

Why not? Because SQL Server is a multiuser environment, so the odds are very, very good that two or more users will end up with the same number, and that leads to serious problems with data integrity later.

You don't need to know what ID is being used until after the data is inserted - and that can be retrieved using SCOPE_IDENTITY (Transact-SQL) | Microsoft Docs[^] once you have inserted the row.
If you need an ID before you INSERT, then don't use an IDENTITY field - use a GUID and supply it from your presentation software rather than letting SQL look after it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900