Click here to Skip to main content
14,603,916 members
Rate this:
Please Sign up or sign in to vote.
See more:
How to lock a record in sqlserver 2005

Hi Friends,

I want to lock the record in sqlserver, so other users cannot change it.

I have a table in SQL with name tbl_SerialNoGenerator
in this i have columnname(SerialNo) which has serialnos from 1001 to 50000

In webpage i have form with label, textboxes and btn which generates this serialNo. using label.

On every pageload it generates serialno. from database and puts on label.
So no. of users uses different forms.

Till here its working fine.

So I need help here, I want to Lock to record. so other users will not generate same serialno.

Please help me, how to lock the record in SQL.

PIEBALDconsult 11-Nov-15 12:03pm
Use a sequence instead; the system will handle the details.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

You'll have to do this manually because you cannot use SQL locks. The reason is when you request the page the server gets the sql data and send it to the client and then disconnects so you no longer have a connection to sql and therefore cannot use sql locks.

You need to implement your own locking. Add a field to the table to indicate who is currently editing the record. Then in your own logic, when someone else tries to access the same record you handle it accordingly.

However, since this is a browser application, there is no perfect way to know for sure that the browser has been closed so you'll want to run some cleanup code to make sure locks don't stay around permanently in case someone closes the browser after locking a record.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100