views:

158

answers:

4

Hi there

I', working on an asp.net app. Is there a way to, when catching a SqlException, to now which constraint was violated?

tks

+1  A: 

You have to add exception handler for the ConstraintException if I am understand your question correctly

try
{

}
catch(ConstraintException exc)
{
//exc.Message 
}
Nagg
A: 

Are you letting the exception bubble up? If you don't catch it and turn custom errors off in the web.config i believe it will display it in your browser. If you are catching it i would put a break point in the catch section and inspect the exception there.

Abe Miessler
+1  A: 

See the answers to this question

sgmoore
+1  A: 

SqlException has a collection of SqlError objects: Errors. The SqlError have properties for error Number and you can compare this with the known constraint violation error numbers (eg. 2627).

While is true that SqlException itself exposes a Number property, it is not accurate if multiple errors happen in a single batch and hence is better to inspect the Errors collection.

Remus Rusanu