7
Answers

Try / Catch - Not catching error

Gustavo

Gustavo

14y
4k
1

Hello:
 
I thought that if there is an error between the 'try' it would execute the 'catch'? Below is my code and its seems its not catching the error on the TestConnection.Open();
 
try
{
SqlConnection TestConnection = new SqlConnection();
TestConnection.ConnectionString =
@"Server=hpw7v64.ggamvpn.gbridge.net;Database=IPDev;User ID=**********;Password=**********;";
TestConnection.Open();   //<<<ERROR - If the server is stopped.
//
SqlDataAdapter TestDataAdapter = new SqlDataAdapter("SELECT * FROM [User]", TestConnection);
DataSet TestDataSet = new DataSet();
//
TestDataAdapter.Fill(TestDataSet);
dataGridViewMSSQLHP.DataSource = TestDataSet.Tables[0];
dataGridViewMSSQLHP.Refresh();
}
catch (SqlException SQLExecute)
{
MessageBox.Show("Connection failed...MSSQL HP" + SQLExecute, "Test Connection...", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

Answers (7)