I have a loop in c# that inserts into a table. pretty basic stuff. Is there something insdie the exception object that\'s thrown when a unique constraint is violated that i
In addition of Bill Sambrone's answer,
Two error codes are used to check unique key violation
2601 - Violation in unique index2627 - Violation in unique constraint (although it is implemented using unique index)Either you can use one or both according to your needs:
try
{
}
catch(SqlException ex)
{
if(ex.Number == 2601)
{
// Violation in unique index
}
else if(ex.Number == 2627)
{
// Violation in unique constraint
}
}
OR
try
{
}
catch(SqlException ex)
{
if(ex.Number == 2601 || ex.Number == 2627)
{
// Violation in one on both...
}
}