Click here to Skip to main content
15,891,423 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi,
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

thanx
Posted
Comments
[no name] 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

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.

Update:
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):
SQL
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
GO
SET LOCK_TIMEOUT 0;
GO
BEGIN TRANSACTION;
GO
UPDATE T SET X=X WHERE X<>X;
GO

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:
SQL
COMMIT TRANSACTION;
GO


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.
 
Share this answer
 
v2
Comments
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.
[no name] 23-Sep-12 0:19am    
Perfect explanation.
+5!

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