Click here to Skip to main content
16,021,041 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how can we generate auto increment when i click on button it will check from database and count than it plus 1 in asp .net using Vb.net Like in this Format
00001
00002
00003
With this code which i Mention it can Auto Generate And Add 1 Plus but format is not Ok for me its generate in this format
1
2
3
But I want In this Format
00001
00002
00003

What I have tried:

Private Sub AutoNumberNo()
       Dim myReader As SqlDataReader
       cn = New SqlConnection(ConfigurationManager.ConnectionStrings("connstring").ConnectionString)
       cn.Open()
       Dim temp As String
       Try
           Dim sql As String = "SELECT MAX(ISNULL(ProdBarCode,0)) 'ProdBarCode' FROM CreateProducts "
           Dim comm As SqlCommand = New SqlCommand(sql, cn)
           myReader = comm.ExecuteReader
           If myReader.HasRows Then
               While myReader.Read()
                   Dim _Count As Integer
                   If (IsDBNull(myReader.Item("ProdBarCode"))) Then
                       _Count = 1
                   Else
                       _Count = myReader.Item("ProdBarCode")
                   End If
                   temp = _Count + 1
                   Me.TextBox4.Text = String.Concat(temp)

               End While
           End If
           myReader.Close()
       Catch ex As Exception
       End Try
       cn.Close()

   End Sub
Posted
Updated 12-Sep-20 5:18am

Don't do it like that! Sql Server is a multiuser system and if you are creating "unique numbers" in your app code, you will get duplicates and they can cause massive problems.

Instead use an IDENTITY column in SQL and let it handle the auto-numbering for you - it will ensure that they are unique.

If you want to display a number with leading zeros, then all you have to do is use ToString with the appropriate format string:
VB
Dim x As Int = 666
TextBox4.Text = x.ToString("D6")
 
Share this answer
 
You NEVER generate ID's in your application code. It should ALWAYS be done in the database.

Do not worry about formatting the numbers in the database. Formatting should be done when values are displayed to the user in the application, not what you see in the database.
 
Share this answer
 
I am not too sure what are you exactly trying here. But, if you have number and you want it in another format, you can convert that into string and apply the needed format.

By default, formatting operations only display non-zero integral digits. You can pad an integer or floating-point number with leading zeros to produce a result string with a specified number of integral digits by using the "0" custom numeric format specifier, as the following example shows


One of the ways to do it:
VB
Module Example
   Public Sub Main()
      Dim value As Integer = 2
      Dim result As String = String.Format("{0,5:00000}", value)
      Console.WriteLine(result)
   End Sub
End Module
'The example displays the following output:
'00002
 
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