views:

93

answers:

5

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 // :)

+1  A: 

If you set the following in web.config then the exception details will be shown in the browser.

<customErrors mode="off" />

Or you could try installing ELMAH, it's very easy to set up, and can be configured to keep a log of exception details in memory.

cxfx
Good idea, I'll check to see the state of that. Thanks // :)
Spanky
+1  A: 

You can enable tracing for your page or application and then do:

Trace.Write ("This is my exception:\n" + exception.ToString ());

Then go to http://yourhost/Trace.axd and you will be able to see the output.

Gonzalo
Cool, thanks // :)
Spanky
+2  A: 

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.

HTH,
Mark

Mark Cooper
That's what I was after, awesome! Thanks // :)
Spanky
A: 

Since you said you can debug, (Assuming you can change the code) why not do a:

string strEx = Ex.ToString(); //Ex is the caught exception.

Then just copy the strEx data from the Visual Studio IDE watch window/Visualizer & then use the data.

Ganesh R.
+1  A: 

If you can access the debugger, you can also do a

Debug.WriteLine(ex.Message)

in your catch statement.

This will write the error to the Output window.

Mark Cooper
Nice, I'll try that. Thanks // :)
Spanky