Click here to Skip to main content
15,889,863 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi. All

I need auto Increment in ID

Start ID : S100

Next time i need S101

Please tell me how i do for this
Posted
Comments
What have you tried so far ?
Navas Khanj 22-Mar-13 15:45pm    
hi..i want Auto INCREMENT ID Number

HEre i use Code

con.Open()
Dim query As String = "Select IsNULL(MAX(cast(SUBSTRING(F_ID,3,len(F_ID))+1,100 )) F_ID from Table2"
Dim dr As SqlClient.SqlDataReader
Dim cmd As New SqlCommand(query, con)
dr = cmd.ExecuteReader
dr.Read()
'txt_FID.Text = dr(("F_ID").ToString)

Dim result = String.Format("R{0:D3}", 100)
txt_FID.Text = result
con.Close(
[no name] 22-Mar-13 15:47pm    
And so what exactly is the problem?
Dave Kreskowiak 22-Mar-13 16:38pm    
Let the database do it and use a composite key. Two columns, the first being your text part and the second being an integer column that autoincrements.

With the code you've written, you can have two seperate users execute this at the same time and come up with the same ID number. That would be bad, wouldn't it??
Navas Khanj 22-Mar-13 15:49pm    
once add ID next time not increment

1 solution

hope help :
you can first get last value in the database then increase it and insert new record !
C#
int Increment;// an int variable will specify the number.
// get last value and save it into Increment.
System.Text.StringBuilder sb = new System.Text.StringBuilder("s");// here u can attach ur custom character
Increment++;//increase variable
sb.Append(Increment.ToString());
//insert new record with increased value
 
Share this answer
 
v2
Comments
Dave Kreskowiak 22-Mar-13 16:37pm    
This doesn't do anything but cause you problems in a production multi-user database application.

A better way to do this would be to let the database do the autoincrementing using a composite key.
ali_heidari_ 22-Mar-13 16:42pm    
you are right but the questin is a custom increment! so do you have any better way to generate custom increment?
i just gave a sugestion to give idea... for multiuser application can first get last ID value of the database and increase it then add new record into database ...
Dave Kreskowiak 22-Mar-13 17:24pm    
for multiuser application can first get last ID value of the database and increase it then add new record into database

No, you can't. There are TWO queries going to the database to do that and they are NOT an atomic operation.

Say you have two clients, each doing the same operation. The first client calls the database to get the last used ID and it is returned dutifully, say as 145. The second client makes the same call, even a couple hundred milliseconds later, and gets, 145. Now they both increment the ID on the client-side and try to write a new record with the new "unique" ID. One works, the other fails.

Trying to write any client-side code that generates unique ID's for a table is huge problem, not trivial. ID's are best left up to the database to assign as it has atomic control over the assigning of record ID's.
ali_heidari_ 22-Mar-13 17:32pm    
but i suppose its not neithier of website or a network based application!
Dave Kreskowiak 22-Mar-13 18:08pm    
Why else would you be generating unique ID's??

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