Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
My stored procedures have error handling as follows:
SQL
BEGIN TRY
--sql UPDATE...
END TRY
BEGIN CATCH 
	declare @msg nvarchar(200)
	SET @msg = ('Error during population of data...')

	SELECT
        	ERROR_NUMBER() AS ErrorNumber,
		ERROR_MESSAGE() AS ErrorMessage
		@msg;
END CATCH


Question:
From the C# code, I execute the SP.
If there is an error in the update query, then the C# code does not seem to ge the error_mesage from the SP.
So, should I use the raiserror or something similar instead of the select in the begin catch section?
Thanks
Posted

use SqlException in Catch section to get Sql error.
C#
try
{
 //Call your SP here
}
catch (SqlException ex)
{
    lblEreMsg.Text = ex.Message;
}
<pre></pre>
 
Share this answer
 
Yes actually you handle exception from your stored procedure. So you are not getting that exception from .NET application. Yes you are correct. If your application need to know the exception then you can use raiseerror function.
Raise Error Details

You can also your throw statement. tsql throw Example

SQL
declare @i int;
begin try
	set @i = 'a';
end try
begin catch
 print 'Error: ' + error_message();
 throw;
end catch
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900