Click here to Skip to main content
15,912,932 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all
Please Help me

I have the Following table

FileCode ... Name
125 .. A
126 .. B
127 .. C

and I have textbox and I want to fill it with the last value in table and increase by one like 128 .. 129 .. etc

I got it but when I try to insert in "FileCode" Column the textbox's value "128"
it got error because other one already inserted in database before me and took the number 128

how can I avoid these errors when two inserting in the same table at the same time
Posted
Comments
Thanks7872 9-Apr-15 9:05am    
Don't pass it from UI(Your code). Make it identity column. See this : https://technet.microsoft.com/en-us/library/aa933196%28v=sql.80%29.aspx

Note : I assume you are using SQL Server as back end.

You have to ways of handling this...

Easiest is to let the database worry about it by using identity column (sequence) and enter it at the insert time not letting users see the number before insert is done.

Second one is harder as you have concurrency to worry about and you have to accept that you might have "holes" in your FileCode sequence

Have one table (say COUNTER) hold current max value of the FileCode
When you get current value, increase it by one immediately. Next person that comes for the number will get that increased number and will also increase it.

The holes come from users that don't finish the insert for whatever reason - they still "used up" the number.

There is third way and that is to show the user one number, but check if it exists before the insert and then change it "on the fly" and then refresh user view, but I think this is bad user experience.

Good luck!
 
Share this answer
 
Try like this:
1)Click[^]

2 )Create a trigger to update the FileCode while inserting Rows in Sql server
 
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