473,387 Members | 1,899 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,387 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 7007
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...

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.