Locking a record being edited by user in Winform application

Sunny Sharma 1 Reputation point
2022-12-22T18:42:45.037+00:00

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

Developer technologies | Windows Forms
Developer technologies | .NET | Other
Developer technologies | C#
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 78,006 Reputation points Volunteer Moderator
    2022-12-22T19:13:22.373+00:00

    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.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.