Groups | Blog | Home
all groups > dotnet ado.net > september 2007 >

dotnet ado.net : SqlException and Violation of PRIMARY KEY constraint


Max2006
9/10/2007 1:06:26 PM
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

Miha Markic
9/10/2007 8:38:47 PM
No, I wouldn't assume that.
Check out SqlException.Errors collection instead.

--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

[quoted text, click to view]
v-wywang NO[at]SPAM online.microsoft.com
9/11/2007 12:00:00 AM
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.
v-wywang NO[at]SPAM online.microsoft.com
9/13/2007 11:41:10 AM
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.
AddThis Social Bookmark Button