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
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
You have to add exception handler for the ConstraintException if I am understand your question correctly
try
{
}
catch(ConstraintException exc)
{
//exc.Message
}
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.
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.