SQL Server 2016 VB Try Statement HResult Error 0x80131904 "Cannot insert duplicate key in object ..."


Using a Try ... Catch statement in VB.NET attempting a SQL insert I receive an HResult code of 0x80131904 (decimal -2146232060). I understand attempting to insert a row where the key value already exists on the database is a violation. What I'm looking for is a definitive way to determine the error verbiage of "Cannot insert duplicate key in object ..." can always be detected by the HResult code of 0x80131904, and, a HResult code of 0x80131904 always means "Cannot insert duplicate key in object ...".

Do I need to interrogate additional structures to determine the specific error verbiage of "Cannot insert duplicate key in object ..."?

My thinking here is the verbiage of "Cannot insert duplicate key in object ..." may change, however, the HResult code (and any other structures I may need to interrogate) will not change.

Any assistance you all might provide would certainly help me.

Much thanks,
David Bach
David BachSoftware DevelopmentAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David Johnson, CD, MVPOwnerCommented:
use and act on the error code the error message may be affected by the users culture settings.
David BachSoftware DevelopmentAuthor Commented:
Greetings David:

I agree with you.

So, does the SQL server HResult code of 0x80131904 always mean "Cannot insert duplicate key in object ...", or does it indicate other errors besides duplicate key errors?

Much thanks, David ...
David Bach
David Johnson, CD, MVPOwnerCommented:
a quick google search shows it  is a generic exception message
login error/connection error/timeout
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

David BachSoftware DevelopmentAuthor Commented:
Greetings David:

I saw this as well.

Is there a definitive way to know via return codes, result codes, reason codes, other structures, etc. when SQL server encounters a duplicate key error without relying on the message text?

Much thanks,
David Bach
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
you probably can refer to error codes:
  • 2601
  • 2627

Errors 2000 - 2999

use master
select * from sysmessages
where description like '%Cannot insert duplicate%'

Open in new window

and need to see if you're able to catch that error code from your existing codes

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
And the answer is no. You'll never get the original error codes directly. Using the error text to search in the DB table master..sysmessages is indeed the only reliable way to get an unique error number you can be sure about what it means.
David BachSoftware DevelopmentAuthor Commented:

Thank you all for your responses. I truly appreciate it.

Ryan, your solution answered my needs directly. I was able to modify existing SQL script and VB code to capture and handle the errors I wanted. I subsequently display text the user can better understand (I hope).

Much thanks for your time and patience ...
David Bach
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.