Mean Sokleap Mean Sokleap - 1 year ago 70
C# Question

Program not responding while connecting to SQL Server

I have a problem. I'm writing window form application in 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 Source

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)

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