473,406 Members | 2,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

SQL Server error constants in ADO.NET

I'm calling a SQL Server statement and I know that it might sometimes return
a "violation of unique key constraint" error (SQL Server error 2627). I'd
like to catch this and handle it - something like this:

Try
cmd.ExecuteScalar()
Catch ex As SqlException
Select Case ex.Number
Case 2627 'Unique constraint violation <-- don't want to
hard code this number
<code to handle violation goes here>
<other cases go here>
End Select
End Try

This is fine, except that I don't want to hard code the value 2627. I
thought there might be some kind of enumeration in ADO.NET that would give
nice names to all these standard errors, but I can't find anything like it.
Is there something I can use, or should I write my own, or just forget about
it and hard-code the error number?

Thanks in advance,
Carl Johansen
http://www.carljohansen.co.uk

Nov 22 '05 #1
2 7008
I am not exactly sure if this can be done as it's more or less provider dependent. I can however suggest you to declare constants in a separate class and use them wherever you would want to catch that specific error code, you will have to hard code the error code only once - during it's declaration. But please be aware of the fact that if you declare your constant in an assembly which is different from the assembly that refers to it, you will have to recompile all those assemblies in case you change the value of the constant and yours is not a public assembly, as the actual value of the constant is substituted by the compiler while compiling.

HTH, Metallikanz!
"Carl Johansen" <carl@_NOSPAM_carljohansen.co.uk> wrote in message news:dd**********@nwrdmz02.dmz.ncs.ea.ibs-infra.bt.com...
I'm calling a SQL Server statement and I know that it might sometimes return
a "violation of unique key constraint" error (SQL Server error 2627). I'd
like to catch this and handle it - something like this:

Try
cmd.ExecuteScalar()
Catch ex As SqlException
Select Case ex.Number
Case 2627 'Unique constraint violation <-- don't want to
hard code this number
<code to handle violation goes here>
<other cases go here>
End Select
End Try

This is fine, except that I don't want to hard code the value 2627. I
thought there might be some kind of enumeration in ADO.NET that would give
nice names to all these standard errors, but I can't find anything like it.
Is there something I can use, or should I write my own, or just forget about
it and hard-code the error number?

Thanks in advance,
Carl Johansen
http://www.carljohansen.co.uk




Nov 22 '05 #2
I am not exactly sure if this can be done as it's more or less provider dependent. I can however suggest you to declare constants in a separate class and use them wherever you would want to catch that specific error code, you will have to hard code the error code only once - during it's declaration. But please be aware of the fact that if you declare your constant in an assembly which is different from the assembly that refers to it, you will have to recompile all those assemblies in case you change the value of the constant and yours is not a public assembly, as the actual value of the constant is substituted by the compiler while compiling.

HTH, Metallikanz!
"Carl Johansen" <carl@_NOSPAM_carljohansen.co.uk> wrote in message news:dd**********@nwrdmz02.dmz.ncs.ea.ibs-infra.bt.com...
I'm calling a SQL Server statement and I know that it might sometimes return
a "violation of unique key constraint" error (SQL Server error 2627). I'd
like to catch this and handle it - something like this:

Try
cmd.ExecuteScalar()
Catch ex As SqlException
Select Case ex.Number
Case 2627 'Unique constraint violation <-- don't want to
hard code this number
<code to handle violation goes here>
<other cases go here>
End Select
End Try

This is fine, except that I don't want to hard code the value 2627. I
thought there might be some kind of enumeration in ADO.NET that would give
nice names to all these standard errors, but I can't find anything like it.
Is there something I can use, or should I write my own, or just forget about
it and hard-code the error number?

Thanks in advance,
Carl Johansen
http://www.carljohansen.co.uk




Nov 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Quinet, Joel | last post by:
Hi all, I use the Win32 extension (Build 157) module with Python 2.3 to add row in a sql server 2000 sp3 DB. My problem is I can not insert data of type money in the DB. I use the following...
12
by: Vince C. | last post by:
Hi, I've created a web application with Visual Interdev and I've added Microsoft XML, version 4 in project's references. A META tag has been successfully added in global.asa: <!--METADATA...
1
by: Carl Johansen | last post by:
I'm calling a SQL Server statement and I know that it might sometimes return a "violation of unique key constraint" error (SQL Server error 2627). I'd like to catch this and handle it - something...
17
by: Steve Jorgensen | last post by:
If you've ever employed custom error numbers and messages in you programs, you've probably ended up with code similar to what I've ended up with in the past something like... <code> public...
7
by: Steve Jorgensen | last post by:
Here is some code to generate code for raising and getting information about custom errors in an application. Executing the GenerateXyzErrDefs procedure generates the code. <tblXyzError>...
2
by: James Lang | last post by:
Hi I am trying to display in an aspx page an image from the employees table in the sql server 2000 Northhwind database "Select photo from employees where employeeid = 1"
0
by: Steve | last post by:
I have a dataset. I fill it with two recordsets from SQL queries. Tables are called tblPlanFYSpendingStage, tblSpendingStage.
1
by: Yi Xing | last post by:
I log into the machine remotely. How do I check the Mac OSX version number under command line? Thanks. hiaips rosedb0 at gmail.com Wed Aug 16 01:23:10 CEST 2006 * Previous message: MySQLdb...
0
by: Stimp | last post by:
I've created an aspx page called HistoryManage.aspx. The page works fine on my local machine but when I load it off the web I get the following strange error... Compilation Error...
0
by: Barry Newberger | last post by:
I am working on a Outlook COM project. For some reason win32com.client.constants quit working between runs of one of my test scripts. It's supposed to acquire attributes for all constants...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.