Automatic generation of the identifier, which is the primary key

in vb.net when entering a new record i want to assign a unique id to the record as in case of numeric, i do this way

Dim ItemID As Integer
KAYAReqConn.Open()
SQLCmd = New SqlCommand("SELECT ISNULL(MAX(ItemID),0) AS ItemID from MstItem", ReqConn)
Dim dr As SqlDataReader

dr = SQLCmd.ExecuteReader
If dr.HasRows Then
    dr.Read()
    ItemID = dr("ItemID") + 1
End If
dr.Close()

      

in this case m using itemid as a unique identifier and the format is 1,2,3 ... and m find out the maximum and assign a new entry, but how to assign if the previous identifier has the values โ€‹โ€‹a00001, a00002, a00003, a00004 ... so Further. how can i create a unique id in this case

+2


a source to share


2 answers


You can get all the IDs, access them one by one, split the ID and only take the whole part, find the maximum of them, generate the next ID and prefix it with .. store it.



It can be a time consuming process, but I haven't found another way.

+1


a source


You are better off using an auto incremental int id field in the database for both of these cases.



For "a0001" you are still better off storing it as an integer, and if "a0000" is for display add it to your code. Or perhaps have a prefix field to store the "a" part - it depends on your requirements.

0


a source







All Articles