Click here to Skip to main content
15,667,864 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi all,

Can anybody tell me how to create an Auto Incrementing number for an sql data field using linq?

Thanks in advance.
Updated 5-Dec-10 22:32pm
Dalek Dave 6-Dec-10 4:12am    
Minor Edit for Grammar and Readability.

You don't need to do it - Sql AutoNumner fields are created and maintained by the database. You cannot assign them in your code - that is the whole point!

Depending on what you are doing, you may want to consider using Guids rather than autonumber - If you insert a record, then have to find it to use it's autonumber field elsewhere, then definitely look at Guid instead. They have a lot of advantages, particularly when it comes to replication.

Autonumber is fine for a log table (and so forth) when you don't need to refer to the record again, but that's about it!
Share this answer
You're best off leaving this to the database - are you using SQL Server?

If so, create a field with an integer data type and set it to Identity = Yes in the designer.

Whenever a new record is added, this field will auto increment.
Share this answer
I would suggest let SQL Server do the incremental numbering by using autonumber.In your scenario if you will do from front end then chances on duplicate inserts due in case of multiuser system.

:) :)
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