开发者

How to Catch Particular DB error in ASP.NET

开发者 https://www.devze.com 2022-12-18 16:32 出处:网络
In my SP (Sql Server 2005) I\'m raising an error using Raiserror (\'No Re开发者_C百科cords Fetched\' , 16 ,1)

In my SP (Sql Server 2005) I'm raising an error using

Raiserror ('No Re开发者_C百科cords Fetched' , 16 ,1)

I want to catch this particular error in ASP.NET.. how do I do it ?


This ought to do it:

catch(SqlException ex) {
    if(ex.Errors.Count > 0 && ex.Errors[0].Message == "No Records Fetched" && ex.Errors[0].Class == 16) {
        // your error
    }
}

However, the errors collection may also contain low-severity print statement messages and other junk from previous statements. It's up to you whether you want to write more sophisticated filtering code to eliminate them.


Whilst Christian Hayter's answer is correct, an alternative would be to use sp_addmessage to create your own customer error number which relates specifically to this problem. This would save you having to parse the error message.

0

精彩评论

暂无评论...
验证码 换一张
取 消