anay
anay

Reputation: 145

stored procedure exception handling in asp.net

Do i need to use try catch in my stored procedure to know where exactly in my procedure error has occured or can i achieve the same if i useSqlConnection.BeginTransaction in my ASP.NET form or simple try catch may be...???

I tried implementing try catch in my stored procedure..i m using sql server 2005 but it does not know TRY and CATCH keywords or ERROR_MESSAGE() function...i also installed sql server 2005 service manager but still it does not work..

if an error is thrown using RAISERROR method will the exception will be shown in my ASP.NET form??

Upvotes: 1

Views: 1705

Answers (2)

Nitai Bezerra
Nitai Bezerra

Reputation: 448

To use TRY...CATCH within SQL Server look this manual: http://msdn.microsoft.com/en-us/library/ms175976.aspx

BEGIN TRY
     { sql_statement | statement_block }
END TRY
BEGIN CATCH
          [ { sql_statement | statement_block } ]
END CATCH

You can catch the exception using catch (SqlException ex). You will find it in the System.Data.SqlClient namespace. You can also check the severity level.

Upvotes: 1

IrishChieftain
IrishChieftain

Reputation: 15253

Surround the DB call in your code with the try-catch... try and put it at the level of the code-behind. If you are using a data access layer, put a try-catch there also, with a single throw statement. Then handle it in the code-behind when it bubbles up.

Upvotes: 1

Related Questions