Mean Sokleap Mean Sokleap - 2 months ago 8
C# Question

Program not responding while connecting to SQL Server

I have a problem. I'm writing window form application in c#.net and connect to SQL Server on Local Network.
if network disconnected my program is not responding. I mean it tries to select data from sql server but network is disconnected. How to catch this error? Thanks in advance.

Answer

You basically need to check for the Open Connection First!

SqlConnection  con = new SqlConnection(Connection_string); //specify your connection string such as Server database etc ...
if (con.State == System.Data.ConnectionState.Open)
 //statements

And you can make an event to Notify that the connection has been closed !