Click here to Skip to main content
15,900,725 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hello friends, i had developed an asp.net project using c# and sql server.
In my project in many of forms i have used auto generated number for transactions, it is working fine on single system, but when i host it on iis and access on more than one system through local network, the same serail number is coming on every system, i'm confused how to block a number which is already using another system,

can somebody give suggestions to get one number should not come in another system which has to be generated automatically.

Thanks.
Posted

I'm just guessing here - since you haven't shown any code that all we can do - but I can infer two things from your question:
1) You are using an SQL Identity field to create the ID number.
2) You are probably getting the current MAX ID value and adding one yourself to generate the "next" ID value, and using that later.

That doesn't work: you cannot "predict" the next value at all, because (as you have found) as soon as you have more than one system "predicting" values, they all start to duplicate and the whole things falls over.
You can only ever be sure of the value immediately the new record has been created: you cannot assume a value at any other time.
 
Share this answer
 
Comments
M.Abdul Rahman 15-Dec-13 6:31am    
Yes I'm using MAX value from database and getting next number till now.

Is there any other good option to do this activity easily?
OriginalGriff 15-Dec-13 6:51am    
No - what you are doing is completely wrong.
You cannot predict the "next" number in advance because a different PC may grab it before you get to use it. You have to wait until the row is actually created, and then return the ID value if you really need it. Until the row is created, the value that ID will have is not defined.
M.Abdul Rahman 15-Dec-13 7:17am    
ok i agree with your point but can you provide a better idea for this?
OriginalGriff 15-Dec-13 7:33am    
Well...you could "preallocate" a record by creating a "dummy" with bad data just to "fix" the ID value, but that will cause more problems than it solves. If anyone doesn't use "preallocated" value it leaves your DB with rubbish in it.

It's a lot, lot better idea to change your software so you only get an ID after the record is saved to the DB, never before (and probably easier too). Think about it: why do you want to know the ID value before you use it? There is no point in telling anyone, because until the record is on the DB they can't do anything with the ID anyway!
Further to Solution 1 by OriginalGriff, the following link on @@@IDENTITY vs SCOPE_IDENTITY() vs IDENT_CURRENT may help:

@@IDENTITY vs SCOPE_IDENTITY() vs IDENT_CURRENT[^]
 
Share this answer
 
I can understand your problem. I have some suggestions for to overcome this problem.

1. You can use identity column to generate auto number.
2. If that is not your solution and if you want to generate custom auto number by your own, you can then build a algorithm which will possibly generate a unique number (you can increase with last number to get a new number ) and then insert the last generated number into a database table. Every time when you require the auto generated number you will only pick the number from the database table and then create a auto number which will replace the earlier number that will solve you problem.


-Thanks,
Mamun
 
Share this answer
 
v4

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