Click here to Skip to main content
15,915,867 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey Guys ,


my Question is the following suppose i have 4 records in the database and they have id 1,2 ,3 ,4 and i deleted the second one so now i have 1 ,3 ,4 so how can i fix this automatically and reorder them to be 1 2 3 .

i am asking this because when i create a new record in my vb.net application i am counting the number of record and incrementing it so now i have a record id of 4 and i can't create this new record because there is a record with that id .

thank you in advance for your help .

regards
Posted

You don't want to do that. What you want to do is instead of using the count + 1 of records to come up with a new id, use a MAX(id) + 1 instead. That is still not a great solution. The proper solution would be to alter the id column of the table into an identity column with an auto increment property and let the db deal with that on its own.
 
Share this answer
 
Comments
FastEvo8 19-Oct-11 15:00pm    
Well said Marcus.
I will also add that trying to reorder the table could become a concurrency nightmare and a performance issue.
If you make id the primary key, you don't need to worry about inserting anything into it. The database will take care of this. If you delete a record, there will still be a missing primary key however.
 
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