Friday, April 10, 2009

Handling Your SQLException By Number

I am working on a bug where I am getting this error: Source: .Net SqlClient Data Provider Description: The server failed to resume the transaction. It is a SQLException from SQL Server, that I want to try/catch in my C# code and handle. The nice thing about SQLException is there is a Number property that you can use to identify the error message from anyother SQL Error. The Number of this exception is 3971 So my C# code looks like this:
catch (SqlException sqlException)
    switch (sqlException.Number)
        case 3971:
The Numbers come from the master.dbo.sysmessages table, one of the ways that I find out what number matches what message is be using a query like this:

FROM master.dbo.sysmessages 
WHERE description LIKE '%The server failed to resume%'
However, it is probably easier to use the debugger, that is if you can catch the error. In this case our bug reporting system was telling me the error, however it is hard to reproduce to "debug" the Number out of it. In that case I use the T-SQL above. {6230289B-5BEE-409e-932A-2F01FA407A92}

1 comment:

  1. Would a switch be the best structure for multiple errors?