473,511 Members | 15,852 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how asp handle primary key violation and re-try insert statement?

The ASP application inserts transaction records in transaction table
with the system time as the primary key. However, it is possible to
have primary key violation because the records in transaction table
come from different sources. The application can show error message and
the user can file a transaction again manually, but I want the
application can have multiple re-tries to perform insert statement
until there is no primary key violation.

Here's the code, but I get stuck what code should i put to re-try the
insert statement, a for loop?and perhaps re-try 3 times before showing
the primary key violation error message to the user?

<%
set conn = CreateObject("ADODB.Connection")
conn.open "<connection string>"
sql = "insert into p_tran values (convert(varchar, GETDATE(), 109),
amount)"
conn.execute(sql)

if err.number <0 then
Response.Write err.description
end if

conn.close
set conn = nothing

%>

Errors
========
Microsoft OLE DB Provider for SQL Server error '80040e2f'
Cannot insert duplicate key row
Please advise. thanks!!

Jul 8 '06 #1
2 3257

"John" <ja*****@gmail.comwrote in message
news:11**********************@s13g2000cwa.googlegr oups.com...
The ASP application inserts transaction records in transaction table
with the system time as the primary key. However, it is possible to
have primary key violation because the records in transaction table
come from different sources. The application can show error message and
the user can file a transaction again manually, but I want the
application can have multiple re-tries to perform insert statement
until there is no primary key violation.

Here's the code, but I get stuck what code should i put to re-try the
insert statement, a for loop?and perhaps re-try 3 times before showing
the primary key violation error message to the user?
No you should change you design and stop using the system time as a primary
key.
<%
set conn = CreateObject("ADODB.Connection")
conn.open "<connection string>"
sql = "insert into p_tran values (convert(varchar, GETDATE(), 109),
amount)"
conn.execute(sql)

if err.number <0 then
Response.Write err.description
end if

conn.close
set conn = nothing

%>

Errors
========
Microsoft OLE DB Provider for SQL Server error '80040e2f'
Cannot insert duplicate key row
Please advise. thanks!!

Jul 8 '06 #2

John wrote:
The ASP application inserts transaction records in transaction table
with the system time as the primary key. However, it is possible to
have primary key violation because the records in transaction table
come from different sources. The application can show error message and
the user can file a transaction again manually, but I want the
application can have multiple re-tries to perform insert statement
until there is no primary key violation.

Here's the code, but I get stuck what code should i put to re-try the
insert statement, a for loop?and perhaps re-try 3 times before showing
the primary key violation error message to the user?

<%
set conn = CreateObject("ADODB.Connection")
conn.open "<connection string>"
sql = "insert into p_tran values (convert(varchar, GETDATE(), 109),
amount)"
conn.execute(sql)

if err.number <0 then
Response.Write err.description
end if

conn.close
set conn = nothing

%>

Errors
========
Microsoft OLE DB Provider for SQL Server error '80040e2f'
Cannot insert duplicate key row

On Error Resume Next
Do Until Err.Number = 0
conn.execute(sql)
Loop

....although this is just a sticking plaster over the gaping wound.
Raising errors is expensive, and using a PK that's likely to raise this
sort of error is poor design. Change your PK to something that's much
more likely to be unique.

--
Mike Brind

Jul 8 '06 #3

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

Similar topics

28
2214
by: Frank Puck | last post by:
Meanwhile there are at least 8 years that compilers exist, which provide a working implementation of C++ Exception Handling. Has anything changed meanwhile? From my point of view nothing has...
39
736
by: Ele | last post by:
Is it correct to say that Whenever a class has a virtual member function, define its destructor as "virtual"? Can a destructor as "pure virtual"? When is it needed to do so? For an interface,...
14
10102
by: Abhi | last post by:
FYI: This message is for the benefit of MS Access Community. I found that this prblem has been encounterd by many but there is hardly any place where a complete solution is posted. So I thought...
7
5941
by: Paul Rivers | last post by:
When I'm trying to write a single feature to an MSI DB using C# there is so little documentation that I have had to piece together everything by hand. Any help would be appreciated I tried in C#...
15
1645
by: john | last post by:
This is a repost, I hope with a better explanation: 1. Create 3 tables, all with 2 fields: Table 1: Field IDnr (Autonumber,key) and Field Test1 (Alpha50) Table 2: Field IDnr (Number,key) and...
7
5859
by: pycraze | last post by:
I would like to ask a question. How do one handle the exception due to Segmentation fault due to Python ? Our bit operations and arithmetic manipulations are written in C and to some of our...
9
2307
by: Bern McCarty | last post by:
I am porting stuff from MEC++ syntax to the new C++/CLI syntax. Something that we did in the old syntax that proved to be very valuable was to make sure that the finalizer would purposefully...
64
3368
by: Zytan | last post by:
I know there are no pointers in C#, but if you do: a = b; and a and b are both arrays, they now both point to the same memory (changing one changes the other). So, it makes them seem like...
12
1858
by: Jeff | last post by:
As I'm learning PHP, I'm making a fair number of mistakes in syntax. In perl, you can turn on reading these errors from the browser by adding this: use CGI::Carp 'fatalsToBrowser'; Is there...
2
1314
jmoudy77
by: jmoudy77 | last post by:
Hi, I've got a database with front-ends at multiple locations that pull from a primary back-end over a satellite network. Each time a front-end closes it triggers a back-up utility that creates a...
0
7242
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
7138
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
7353
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,...
1
7075
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...
1
5063
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...
0
4737
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...
0
3222
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...
0
1572
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
781
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.