Get error raised from sql command?

  • Thread starter Thread starter Smokey Grindel
  • Start date Start date
S

Smokey Grindel

Is there a way to get an error that is raised from a sql server stored
procedure when executed from a sqlcommand object? thanks!
 
I think you misunderstood me... I already am raising errors in the stored
proc's... I want to know how I can find out what the error was when I ran it
inside of ADO.NET so I can respond to the error in my program
 
Smokey said:
I think you misunderstood me... I already am raising errors in the stored
proc's... I want to know how I can find out what the error was when I ran it
inside of ADO.NET so I can respond to the error in my program
What severity are you using in your raiserror? The following causes a
System.Data.SqlClient.SqlException to be raised in the client:

raiserror('Error Message', 17, 1)
return

The exceptions message property is "Error Message" in this case.
 
try this in your stored proc

RAISEERROR(N'This is a message %s %d', -- message text
16, --severity
1, --state
N'number', --first argument
5); --second argument

and in your code you can get this error message in the SqlException object


try
{
ExecuteSP();
}
catch(SqlException ex)
{
Console.WriteLine(ex); //ex.Message will have the message text you used in
raiseerror
}
 
Back
Top