I am developing a windows application, where multiple users can uses the same database.
My problem is when insert a new record to database table, I need to display the new customerID
in the customer registration form. For that I get the last customer id and increment by one and display the customer ID of the new customer. In a multi-user environment, if two people are trying to add a new customer at a same time, then there will be problem displaying the new customer id. And when two users accessing and updating the same record at a same time.
What to do?
Don't get the Last Customer Id and auto increment it by one, it's not safe for the reason you mentioned.
After the insert statement of the new record just select the inserted id and display this to the customer registration form with this line of code:
SELECT SCOPE_IDENTITY()
Or check this select statement which does the equivalent:
SELECT @RecordId= MAX([RecordId])
FROM [dbo].[CustomerTbl]