Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello friends

I am in deep trouble. I want to restrict user when other user perform operation on particular row of sql server datatable. Only in asp.net, because i know we can do it in sql server using locking technique.

Guys please help me

Thanks
Posted
Comments
Mehdi Gholam 8-Sep-11 1:49am    
Locking tables is not advisable, if the operation is done in a transaction there should be no problem, what issue are you having?

I'm not exactly sure why you want to do this - it is really difficult to work out that another user is currently performing an operation, since browsers do not normally check back with the server before they start anything.

But, probably your best solution is to either:
1) Do your updates in a stored procedure so that a set of updates is complete (and could if necessary be verified before they are performed)
2) Provide some manual locking of records, with an expiry date - much the same as happens here with improving questions. If you go to a Q&A question here, sometimes there is a message that "this question is locked for editing by xxx. The lock expires in nn minutes". Until user xxx has committed or canceled his changes, or the time elapses, the "Improve question" widget is not available. If you press the "Improve question" widget and user xxx got there between the pafge load and your press, you get an error to let you know.

What you do, will depend on what kind of updating you are trying to do.
 
Share this answer
 
Comments
[no name] 8-Sep-11 4:01am    
Nice ans... my5!
sujitdeshpande 9-Sep-11 0:28am    
Thanks Maulik It really helps me to improve knowledge, i will definitely use this in my project
If I understood your question correctly and if you're talking about row level locking, it doesn't matter what your client is. If it's ASP.NET, a Windows Forms application or even SQL Server Management Studio, all these follow the same locking rules: When a row is modified, it's automatically locked and no one can make modifications to that row while the lock is in place.

So if that is your concern, you don't have to worry about it. But instead you should worry about the lock duration. If you're executing several SQL statements in a single unit of work you have to make sure that the locks are not released until the end of the unit of work. To accomplish this you need to use transactions.

In the beginning of the execution you start a transaction using SqlConnection.BeginTransaction[^]. Then you execute all the statements needed and in the end, if everything goes fine, you call SqlTransaction.Commit[^]. If something goes wrong you call SqlTransaction.Rollback[^]. This will ensure that no one can make modifications to the same rows that you're currently processing.

If you don't use transactions each SQL statement will be automatically committed (that is, if it succeeds) when the command ends. This may result to problematic situations since for example logical cross table integrity cannot be enforced anymore.
 
Share this answer
 
Comments
sujitdeshpande 9-Sep-11 0:29am    
Thanks Mika .It really helps me to improve knowledge, i will definitely use this in my project
Wendelius 9-Sep-11 0:55am    
You're welcome :) If you like you can mark the asnwers that were helful as answered.

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