views:

37

answers:

2

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

Raiserror ('No Records Fetched' , 16 ,1)

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

+1  A: 

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.

Christian Hayter
A: 

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.

Ed Harper