Reputation: 568
I got this code:
try
{
using (SqlConnection sourceCnx = new SqlConnection(SOURCE_CONN_STRING))
{
sourceCnx.Open();
SqlCommand sysCmd = sourceCnx.CreateCommand();
sysCmd.CommandText = "My query";
sysCmd.ExecuteNonQuery();
}
}
catch (Exception)
{
//Throwing a message box informing that there is an error
}
I want to display a message when the user is no longer connected to the internet. But when I debug my program without an internet connection, the program crashes with a SqlException. (The "catch" block does not catch the exception)
I tried to add catch (SqlException) { // code }
before catch (Exception)
but it doesn't work.
I still have an exception instead of a message displayed by the catch block.
I don't know what to do because if I create a method to test the internet connection (try
to ping google.com) then return true if it's ok, it will be the same : I got an exception because of no internet connection.
Any idea?
Upvotes: 0
Views: 1213
Reputation: 330
I think you should get the BaseException
catch (Exception exp)
{
if (exp.GetBaseException() is System.Data.SqlClient.SqlException)
{
var sqlException = exp.GetBaseException() as System.Data.SqlClient.SqlException;
if (sqlException != null && sqlException.Number == 547)
{
//do something
}
}
//do something
}
Upvotes: 0
Reputation: 119186
You have set your environment to always break when a CLR exception is thrown. You can leave it like that if you wish and press F5 to carry on execution of your program. Or you can turn this off (it is switched off by default):
Go to Debug
menu, select Exceptions
and ensure Common Language Runtime Exceptions
is not checked.
Upvotes: 1
Reputation: 64
In the part of the catch code, you have to set a variable not? And after that take any of her property.
For example:
try
{
using (SqlConnection sourceCnx = new SqlConnection(SOURCE_CONN_STRING))
{
sourceCnx.Open();
SqlCommand sysCmd = sourceCnx.CreateCommand();
sysCmd.CommandText = "My query";
sysCmd.ExecuteNonQuery();
}
}
catch (Exception ex)
{
//example
MessageBox.show(ex.message);
}
Upvotes: 0
Reputation: 708
Try with this:
try
{
using (SqlConnection sourceCnx = new SqlConnection(SOURCE_CONN_STRING))
{
sourceCnx.Open();
SqlCommand sysCmd = sourceCnx.CreateCommand();
sysCmd.CommandText = "My query";
sysCmd.ExecuteNonQuery();
}
}
catch(SqlException sqlEx)
{
MessageBox.Show("there was a sql issue");
}
catch(Exception ex)
{
MessageBox.Show("there was some other issue");
}
Upvotes: 1
Reputation: 6390
You might want to put the exception handing inside the using block, like this:
using (SqlConnection sourceCnx = new SqlConnection(SOURCE_CONN_STRING))
{
try
{
sourceCnx.Open();
SqlCommand sysCmd = sourceCnx.CreateCommand();
sysCmd.CommandText = "My query";
sysCmd.ExecuteNonQuery();
}
catch (SqlException e)
{
// This will catch any SQL Exceptions.
// Use "throw;" if you want to rethrow the exception up the stack
}
catch (Exception e)
{
// This will catch any other exceptions.
// Use "throw;" if you want to rethrow the exception up the stack
}
}
Assuming that you have actually replaced "My Query" with something specific to your scenario, the SqlException is most likely down to your machine not being able to see the SQL Server instance. Try pinging it...
Upvotes: 1