I am looking for the best practice to handle sql server availability from within a windows form application using C# and MS SQL Server 2012
currently when my application starts, I have a DGV which is populated from a stored procedure, everything works but I am trying to bulletproof my app.
For example: When I change my Data Source name to something that doesn't exist, I get an exception after few seconds and my main form won't display for that whole time making the end user wondering what is going on before seeing the exception, what do you guys do to handle unavailable sql server? or unplugged Ethernet connection?
The answer I am looking for is to have some kind of a Watchdog timer that will either start retrying when there is a problem or keeps constantly checking in the background and will display some banner to the user once connection is down
here is my SP code:
public DataTable GetProducts()
using (SqlConnection conn = new SqlConnection(m_ConnectionString))
cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "Stored Procedure Name";
cmd.CommandTimeout = SQL_TIMEOUT;
// Create a data adapter
da = new SqlDataAdapter();
da.SelectCommand = cmd;
// Fill data table
dt = new DataTable();
catch (Exception ex)
MessageBox.Show("The following error has occured: " + ex.Message, "GetProducts", MessageBoxButtons.OK, MessageBoxIcon.Error);
// Exit the application gracefully
Does the timer have to be in its own thread? basically I would like the user to still load the main form with empty grid and controls and a timer that is trying to establish connection in the background.
Please let me know