//Create the Connection object
string connectionString = WebConfigurationManager.ConnectionStrings["Northwind"].ConnectionString;
SqlConnection con = new SqlConnection(connectionString);
try
{
//Try to open the connection.
con.Open();
lblInfo.Text = "<b>Server Version:</b>" + con.ServerVersion;
lblInfo.Text += "<br/><b>Connection Is:</b>" + con.State.ToString();
}
catch(Exception err)
{
//handle an error by displaying the infromation.
lblInfo.Text = "Error reading the datebase." + err.Message;
}
finally
{
//Either way ,make sure the connection is properly closed
// even if the connection wasn‘t opened successfully
//calling close() won‘t cause an error.
con.Close();
lblInfo.Text = "<br/><b>Now Connection Is:</b>" + con.State.ToString();
}