SqlException and Violation of PRIMARY KEY constraint

M

Max2006

Hi,

At this point I check the SqlException.Message to check for the "PRIMARY
KEY" errors.

Can I use SqlException.ErrorCode to do the job? I have
ErrorCode=-2146232060 for primary key errors. Can I assume that the error
code -2146232060 and primary key error are always associated together?

Thank you,
Max
 
W

WenYuan Wang [MSFT]

Hello Max,

I agree with Miha.
Primary Key error is not associated with
SqlException.ErrorDode(-2146232060).
We have to check SqlException.Error property.

2627 is related to Primary key.
Violation of %ls constraint '%.*ls'. Cannot insert duplicate key in object
'%.*ls'.

catch (SqlException ex)
{
if (ex.Number.Equals(2627))
{
// Display your error here
}
}

Dan asked the same issue in asp.net forum before.
You may check http://forums.asp.net/p/1144699/1850898.aspx#1850898
[Catching Primary Key Violation on insert error]

Hope this helps.
Best regards,

Wen Yuan
Microsoft Online Community Support
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
W

WenYuan Wang [MSFT]

Hello Max,

This is Wen Yuan, again. Have you resolved the issue so far?
If there is anything we can help with, please feel free to let me know.
We are glad to assist you.

Have a great day,
Best regards,

Wen Yuan
Microsoft Online Community Support
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top