473,324 Members | 2,002 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,324 software developers and data experts.

Catch duplicate primary key exeption

ad
The code below can only catch common excption.
I want to catch duplicate primary key exeption and do something more.

How can I catch duplicate primary key exeption

try
{
Insert a record to table
}
catch (????)
{
Do some thing more
}
catch (Exception err)
{
Label1.Text=err.Message;
}
Nov 19 '05 #1
4 3301
You catch a SqlException. Although that can be thrown for any number of
reasons. You would have to examine the collection of errors inside it, and
see the number to see exactly why the exception was thrown.

"ad" <ad@wfes.tcc.edu.tw> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
The code below can only catch common excption.
I want to catch duplicate primary key exeption and do something more.

How can I catch duplicate primary key exeption

try
{
Insert a record to table
}
catch (????)
{
Do some thing more
}
catch (Exception err)
{
Label1.Text=err.Message;
}

Nov 19 '05 #2
How is the primary key created? Is it auto generated? If so, you
wouldn't have this problem, so I am guessing that you are generating it
yourself. Can you tell us how the key is generated? You also need to
tell us how you are doing the insert? Is all of this done in code,
i.e., do you create a key in code and then try to do an insert? Or do
you use stored procedures for all of this? Tell us a little bit more...

sayed

Nov 19 '05 #3
ad
Thanks,
It is a key like social insurance ID.
It will be keyin by user.

<ha**********@gmail.com>
???????:11**********************@g44g2000cwa.googl egroups.com...
How is the primary key created? Is it auto generated? If so, you
wouldn't have this problem, so I am guessing that you are generating it
yourself. Can you tell us how the key is generated? You also need to
tell us how you are doing the insert? Is all of this done in code,
i.e., do you create a key in code and then try to do an insert? Or do
you use stored procedures for all of this? Tell us a little bit more...

sayed

Nov 19 '05 #4
If you are doing the insert via a stored procedure, you can do a
"count(id) from mytable" and see if that is greater than zero. If so,
do a "raise error 'social insurance id already exists'". If you are not
inserting with a stored procedure, you have to do the count statment or
a select statement to figure out if the social exists and throw a
similar exception.

sayed

Nov 19 '05 #5

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

Similar topics

0
by: Gena | last post by:
Hi, Can anyone help me to increment the primary key's value on duplication without using mysql v 4.1 "on duplicate key" function? I can't upgrade to use the "on duplicate key" feature so am now...
18
by: Elroyskimms | last post by:
I have a table using an identity column as its Primary Key and two columns (table reduced for simplicity) EmployeeNumber and ArrivalTime. CREATE TABLE ( IDENTITY (1, 1) NOT NULL , (10)...
2
by: stranger | last post by:
My database is set up so people can input parts orders. Sometimes they order the same parts on a monthly basis. I want to be able to duplicate past parts orders and have it pasted in with a new...
0
by: Enzo Marinelli | last post by:
Hello everyone I have a HUGE database, with very complex stored procedures that handle every possible error and the passed it to an ASP site with RAISERROR Now I need to use that same database...
3
by: stuart.d.jones | last post by:
Hi, I have a DetailsView control bound to a SqlDataSource. I've got everything working nearly as I want, but here's my problem. I have a table which has a Guid as a Primary Key, and a VarChar...
5
by: Manish | last post by:
The topic is related to MySQL database. Suppose a table "address" contains the following records ------------------------------------------------------- | name | address | phone |...
2
by: Hetal | last post by:
I searched online and went through the forums as well, but i could not find a way to capture the database primary key violation exception. Any help will be much appreciated. Thanks, Hetal
2
by: embarkr | last post by:
I am getting the error: "Violation of PRIMARY KEY constraint 'PK_tblCustomsTariffTreeMap'. Cannot insert duplicate key in object 'dbo.tblCustomsTariffCodeTreeMap'." However, the record I am...
6
by: Dave | last post by:
I really don't like the users getting an unhandled expception page, and I'm still to new with ASP.Net and C#. So please accept my appology for the 2 part question. SqlException (0x80131904) ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.