Locking a record being edited by user in Winform application

Sunny Sharma 1 Reputation point

We have built a Winform application on Dotnet core 6. There is a requirement to lock the record been edited by a user so that if the other user try to edit the same record, they should get a message like "Record is currently been edited. Try after some time" until the first record is edited and saved in database successfully. Is this something that can be achieved in Winform application? Please suggest

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,388 questions
Microsoft Technologies based on the .NET software framework.
858 questions
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
7,496 questions
No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 30,866 Reputation points

    sure, there are several approaches. I typically create a lock resource table, an edit adds a row to the resource table, an update deletes the row. you just need to decide on the unique resource id and key for the record..

    I lock per resource id allowed at a time. make the source id and key a unique key.

    create table ResourceLock   
          ResourceType int not null,      // row type  
          ResourceKey varhchar(50) not null, //row key  
          LockTime datetime not null default CURRENT_TIMESTAMP, //when locked  
          LockUser varchar(132) not null // who locked - can use a transaction guid if no user   

    typically a checkout (read for edit) create the row or gives lock error, an update checks that the row exists for the user and reports broken lock. I usually allow max lock time or users allowed to break locks.

    a variant is create read locks on read, and only allow update if user has read lock, first update deletes all locks, be sure to use high enough isolation level for this.