Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4033764
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:53:15+00:00 2026-05-20T11:53:15+00:00

OK so I have read a fair amount about SQL Server’s locking stuff, but

  • 0

OK so I have read a fair amount about SQL Server’s locking stuff, but I’m struggling to understand it all.

What I want to achieve is thus:

I need to be able to lock a row when user A SELECTs it

If user B then tries to SELECT it, my winforms .net app needs to set all the controls on the relevant form to be disabled, so the user can’t try and update. Also it would be nice if I could throw up a messagebox for user B, stating that user A is the person that is using that row.

So basically User B needs to be able to SELECT the data, but when they do so, they should also get a) whether the record is locked and b) who has it locked.

I know people are gonna say I should just let SQL Server deal with the locking, but I need User B to know that the record is in use as soon as they SELECT it, rather than finding out when they UPDATE – by which time they may have entered data into the form, giving me inconsistency.

Also any locks need to allow SELECTs to still happen – so when user B does his SELECT, rather than just being thrown an exception and receiving no/incomplete data, he should still get the data, and be able to view it, but just not be able to update it.

I’m guessing this is pretty basic stuff, but there’s so much terminology involved with SQL Server’s locking that I’m not familiar with that it makes reading about it pretty difficult at the moment.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T11:53:16+00:00Added an answer on May 20, 2026 at 11:53 am

    This isn’t really what SQL Server locking is for – ideally you should only be keeping a transaction (and therefore a lock) open for the absolute minimum needed to complete an atomic operation against that database – you certainly shouldn’t be holding locks while waiting for user input.

    You would be better served keeping track of these sorts of locks yourself by (for example) adding a locked bit column to the table in question along with a locked_by varchar column to keep track of who has the row locked.

    The first user should UPDATE the row to indicate that the row is locked and who has it locked:

    UPDATE MyTable 
    SET `locked`  = 1
    AND `locked_by` = @me
    WHERE `locked` = 0
    

    The locked = 0 check is there to protect against potential race conditions and make sure that you don’t update a record that someone else has already locked.

    This first user then does a SELECT to return the data and ensure that they did really manage to lock the row.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Alright...I've given the site a fair search and have read over many posts about
I am fairly new to Perl programming, but I have a fair amount of
I have read about partial methods in the latest C# language specification , so
I have read this post about how to test private methods. I usually do
I have read the documentation on this and I think I understand. An AutoResetEvent
I've noticed that google app engine seems to have a fair amount of downtime
I have read all the blog posts on digital signing and checked out GoDaddy
I have read several interesting C#/F# comparisons here on stackoverflow. So, first of all,
I have a fair amount of experience with the DataContractSerializer and handling custom type
I have a project where I need to construct a fair amount of configuration

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.