I have a .aspx page calling a .asmx page's web service. In that .net web service I attempt to open a database connection to SQLServer 2008. The connection is failing. I am not sure why. I am doing this in a try / catch and the catch does get hit when I debug. I'm not sure what I can output there though as I don't have access to the server's filesystem to write a log file.
I found this posting:
try
{
SqlCommand cmd =
new SqlCommand("raiserror('Manual SQL exception', 16, 1)",DBConn);
cmd.ExecuteNonQuery();
}
catch (SqlException ex)
{
string msg = ex.Message; // msg = "Manual SQL exception"
}
here and it might do the trick for me, but I don't know how to make the msg string output to the page which called this web service? Is there a way to propagate it up the exception chain by having the calling page also implement that same exception handler?
Thanks // :)
Your should be able to trap the SQL Exception as a specifc type and read the particular error message:
try{
...
}
catch(SQLException sqlex)
{
/// do some work here with sqlex.Message
}
catch(Exception ex)
{
/// this will trap any other 'type' of exception incase a sqlex is not thrown.
}
You could then throw this "up the stack" which means sending it back to the method which called the failing code:
catch(SQLException sqlex)
{
throw sqlex;
}
Or you could throw a new message based on the sql exception:
catch (SQLException sqlex)
{
throw new Exception("My method threw an exception in SQL:" + sqlex.Message);
}
All of these approaches allow you to send messages back up the stack to the client that called the SQL. This is where you should render you message.