简体   繁体   中英

SQL raiserror output in c# asp.net

I have a stored procedure that for testing purposes always throws an error.

UPDATE [Test]
    SET  [1.1] = 0
        ,[1.2] = 0
        ,[1.3] = 0
        ,[1.4] = 0

    WHERE Record_ID = 2 AND
          [1.1] = 0 AND
          [1.2] = 0 AND
          [1.3] = 0 AND
          [1.4] = 0
if(@@ROWCOUNT = 0)
    begin 
    raiserror('Error on purpose',16,1)
END

While the error gets thrown in SQL MGMT studio, it does not get triggered in try/catch block in asp.net.

I call the SP like so:

try {
     dataContext.TESTProcedure();
}
catch(Exception ex)
{
    string error = ex.Message;
}

I have stepped through the code and it never enters the catch even though it should.

What is going wrong?

I would go deeper into what dataContext is doing. If behind the scenes it is using a datareader, then one may have to advance to the next result set to get the error.

Call

NextResult

on the reader. For more information:

"So how do we get this to work? Well, when using a SqlDataReader it will only iterate over the first result set, which in this case will be the select in the stored procedure. This result set will be empty since there is no data for ID number 10. So nothing is displayed and the second result set is not accessed. What we have to do is to move into the next result set in order to get hold of the error.

One way to do this is to as follows, ie, this will keep going over all the returned result sets until the SqlDataReader is closed, which it will be when there are no more result sets returned."

http://blogs.msdn.com/b/spike/archive/2009/04/06/catch-a-sql-server-raiserror-at-client-side-using-sqldatareader.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM