Click here to Skip to main content
13,147,383 members (33,744 online)
Rate this:
Please Sign up or sign in to vote.
See more:
i made an application in c#,and installed the exe on each client computer within a lan, that is actually sharing a common database on server , and now i want to check for that if some one is accessing challan form then no user can open that form until closed, plz help me out

Posted 22-Sep-12 2:13am
Wes Aday 22-Sep-12 8:21am
And what kind of help is it that you are expecting? How to connect to a database? How to show a form? How to read a database? How to write to a database? How to restructure your program so that it makes sense?

1 solution

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

Solution 1

There is no built-in solution for this approach. But you can use the database (a dedicated table, or a shared table that-s involved in this lock logically). Assuming you use SQL Server proper lock hint and transaction isolation can be used to achieve the same goal (see details here[^]) - see SERIALIZABLE. You might find more details in these articles: Using Transactions in ADO.NET[^], Locks and Duration of Transactions in MS SQL Server[^].
The idea: if an object is locked, only one application can gain access to it, and you can use this behavior to control weather a form can or can not be displayed.
If you take this path, be aware to test it in cases when on client gathered the lock, but was cut from the server (killed), you might need some fine-tuning. But even so, this approach is easier to code than other distributed IPC.

Here is the scenario you can use to gain and test exclusive access to any table (a production one, or a dedicated one) - this is the T-SQL version, but it is as simple to do it in C# also.
Ok, first of all, before you want to show that editor form, issue this script (or it's c# equivalent):

Please remark the dummy DML statement: it does nothing but locks the table T.

After the user finished editing, issue the actual update DML statements, and simply commit the transaction:

What happens? With serializable transaction, and the update issued the client will gain exclusive access to the T for the whole transaction - until it is committed, rolled back or canceled by the server itself due to lost connection. And because it is a zero lock wait timeout, the second application instance trying to get the same lock, will fail immediately. You will get an exception on client side, that you can use to warn the user to come back later, since the table he wants to edit is edited by somebody else. After commit (or rollback) the lock is released, so an other instance can get the lock and so on.

For C# version, see SqlConnection.BeginTransaction[^], as I know SET LOCK_TIMEOUT has to be issued as a nonquery command.

You can also fine-tune the timeout value if you can allow waiting. By default it is -1 (infinite) - thus any lock could hang your application.
Member 8943376 22-Sep-12 8:41am
but if i am using a dedicated table then it may be the situation occur when the
application hangs r stop working and the update was not to set that table then it may be a problem when another computer user want to access then always he got the old status so he cant access to that form
Zoltán Zörgő 22-Sep-12 12:51pm
See my update.
Meysam Tolouee 23-Sep-12 0:19am
Perfect explanation.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.170915.1 | Last Updated 22 Sep 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

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