Click here to Skip to main content
13,256,123 members (27,982 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


6 bookmarked
Posted 19 Dec 2013

Automatic Numbering for the Primary Key Column

, 19 Dec 2013
Rate this:
Please Sign up or sign in to vote.
Automatic numbering for the primary key column


Many of you might have come across an issue of Auto incrementing while working with database products. Truly speaking, recently I also came across one silly problem and thought of sharing it here.

Imagine that at some point of time, you might want to send your new data to a back-end database. If your application supplies the auto-increment values, then

  • what will the database do?
  • what if application receives duplicate values from different client applications?

The answer is that these auto-increment values are never sent to the database because the auto-increment column in the database table will provide a value whenever a new row is added. After each new row is added, the back-end database table generates a new-increment number and then your application will query the database to get the newly created number. Your application will then update its primary key number to the values that came from the database. This means that all the foreign key references will need to be updated as well.

Another problem, what happens if you add new rows in your application to generate auto-increment values of 1 to 100 and then send these rows back to the database table, and the table already has 10 rows???

When the first row is sent from your application, it has an auto-increment value of 1. The new auto-increment number created in the database will be 11. Your application queries for the 11 and tries to change the 1 to 11. Guess, what will happen here? Of course, an EXCEPTION will be thrown because 11 is already in your table.

The solution to this problem is, set AutoIncrementSeed to -1 and set AutoIncrementStep to -1. This will cause negative numbers to be generated; they won't conflict with the values coming from the database because the database doesn't generate negative numbers.

Hope the above small tip will save your lot of time in debugging the code to find the root cause. Enjoy learning !!!


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


About the Author

You may also be interested in...

Comments and Discussions

QuestionSolution from ADO.NET Pin
Muhammad_Shoaib19-Dec-13 20:22
professionalMuhammad_Shoaib19-Dec-13 20:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171114.1 | Last Updated 19 Dec 2013
Article Copyright 2013 by Shweta Lodha
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid