I’d like to get the message number and severity level information from SQL Server upon execution of an erroneous query.
For example, when a user attempts to delete a row being referenced by another record, and the cascade relationship is “no action”, I’d like the application to be able to check for error message 547 (“The DELETE statement conflicted with the REFERENCE constraint…”) and return a user friendly and localized message to the user.
When running such a query directly on SQL Server, the following message is printed:
Msg 547, Level 16, State 0, Line 1
<Error message...>
In an Asp.Net app is this information available in an event handler parameter or elsewhere?
Also, I don’t suppose anyone knows where I can find a definitive reference of SQL Server message numbers?
Sure – check out the SqlException that gets thrown is something goes wrong on SQL Server.
It contains a collection of SqlError elements, which contain a bunch of properties, including
ClassGets the severity level of the error returned from SQL Server.LineNumberGets the line number within the Transact-SQL command batch or stored procedure that contains the error.MessageGets the text describing the error.NumberGets a number that identifies the type of error.ProcedureGets the name of the stored procedure or remote procedure call (RPC) that generated the error.ServerGets the name of the instance of SQL Server that generated the error.SourceGets the name of the provider that generated the error.StateGets a numeric error code from SQL Server that represents an error, warning or “no data found” message.