473,626 Members | 3,183 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.ExecuteScal ar()
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 7020
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_c arljohansen.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.ExecuteScal ar()
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_c arljohansen.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.ExecuteScal ar()
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
1901
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 command to create the parameter : prm = cmd.CreateParameter('Open', constants.adCurrency, constants.adParamInput, 8, 12.10) cmd.Parameters.Append(prm)
12
3088
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 TYPE="TypeLib" NAME="Microsoft XML, v4.0" UUID="{F5078F18-C551-11D3-89B9-0000F81FE221}" VERSION="4.0"--> But when I write the following code in an ASP server-side JavaScript:
1
1192
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 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
17
7450
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 Const xyzcErrNumBase As Long = 500 Public Const xyzcErrNumHeyDontDoThat = xyzcErrNumBase + 1 Public Const xyzcErrTxtHeyDontDoThat = "Hey! Don't do that!"
7
2702
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> ErrorCodeOffset (Long Int) ErrorObjBaseName (Text(50)) ErrorDescription (Text(255)) 1 DontDoThat Hey! Don't do that!
2
2397
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
1425
by: Steve | last post by:
I have a dataset. I fill it with two recordsets from SQL queries. Tables are called tblPlanFYSpendingStage, tblSpendingStage.
1
3368
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 installation error * Next message: What would be the best way to run python client in the background
0
1735
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 Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
0
1284
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 defined in loaded COM servers through its __dicts__ attribute,and did for awhile Now it is throwing an AttributeError:
0
8196
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8637
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8364
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7193
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5574
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4092
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4197
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1808
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1511
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.