views:

1185

answers:

3

How can I store SQLSERVER's raiserror message in C# ?

Thanks in advance

A: 

Did you try a search?

This is a pretty common question. It's best to try finding it for yourself before you take the time to make a post.

Aaron Alton
I believe that asking a simple/common questions are encouraged as long as it's not a duplicated on StackOverflow (according to SO podcast)
Sung Meister
+4  A: 

It depends on the severity of your RAISERROR. Severities 1-10 are considered information messages and do not break the flow of your C# client, ie. they don't throw exception. These information messages will trigger the InfoMessage event on your connection.

If you RAISERROR with severity between 10 and 16 is considered an error and your SqlCommand.Execute will throw and exception you can catch.

Severities above 17 are not for you to play with, they are only to be used by the engine to indicate severe problems that may take the database offline or shutdown the instance. Is not technically possible to raise errors with severity above 25, your example with severity 100 is just silliness.

Remus Rusanu
The OP has not done the most basic research. Don't encourage him. Also, the state will fail because it is 8 but unsigned and @@error is between 1 and 65535.
gbn
+2  A: 

Have a look at:

http://www.sommarskog.se/error-handling-I.html#ADO.Net

In particular: the SqlClient section. The SqlError class gives you all the information you need.

Sam Saffron