Click here to Skip to main content
15,895,192 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
<pre lang="C#">I have retrieved the last ID incremented by 1 into a textbox on form load, but is there a way to reserve a place in the table for this ID (that is retrieved), until form is saved?? Because there are multi users using this form and i need to get an ID once form is loaded, although form might take more time to be saved.
Am using c# for my application,i don't know if i have to implement this on my code application or in database itself.



What I have tried:

i only retrieved the last id incremented by 1 to use the ID.
Posted
Updated 19-Jun-16 21:09pm

1 solution

Don't.
Never, ever do this: it leads to all sorts of horrible data problems which are a nightmare to sort out.
When you "preallocate" ID values, there is a huge risk that two different users will try to use the same ID, and then the data for the two gets "mixed" into the DB. Sorting out what data belongs to what can take ages, particularly as such problems aren't usually spotted immediately - and by the time they are, huge chunks of your DB are a mess and no-one remembers what should be where.
Only ever allocate ID values when you store the information - and at that point let the DB sort it out by using an IDENTITY column.
 
Share this answer
 
Comments
ramy nemer 20-Jun-16 3:45am    
the ID is incremented by 1 so no duplicate ID's is allowed, but there is a way to do that
OriginalGriff 20-Jun-16 4:19am    
It may be incremented, but *if it's incremented on two different machines* they get the same value... And if I increment it on my machine now, but go to lunch what happens on yours? And if I go home and turn the computer off what value do I get tomorrow?
Trust me on this: there are ways to do what you want, but they are over complicated and prone to faults. You don't need the ID value in advance: what would anyone do with it in the real world? Do you users care? No - it's an internal detail they don't need. Let the DB take care of it when you INSERT the data. It's simpler, safer, and a lot more effective.
ramy nemer 20-Jun-16 6:55am    
yeah it make sense but am working on it to ensure security and safety. Thanks for your advice

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