473,804 Members | 2,715 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("A DODB.Connection ")
conn.open "<connectio n string>"
sql = "insert into p_tran values (convert(varcha r, GETDATE(), 109),
amount)"
conn.execute(sq l)

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 3278

"John" <ja*****@gmail. comwrote in message
news:11******** **************@ s13g2000cwa.goo glegroups.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("A DODB.Connection ")
conn.open "<connectio n string>"
sql = "insert into p_tran values (convert(varcha r, GETDATE(), 109),
amount)"
conn.execute(sq l)

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("A DODB.Connection ")
conn.open "<connectio n string>"
sql = "insert into p_tran values (convert(varcha r, GETDATE(), 109),
amount)"
conn.execute(sq l)

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(sq l)
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
2263
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 changed -- people are still using a coding style, which I call pre-C++-Exception-Handling. Is this different in your experience? What is your experience regarding this subject? I think that a C++ IOstream lib, which is by standard free of exceptions...
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, Interf: class Interf { public:
14
10147
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 I should give back to the community by posting our findings. Thanks you all for all your help till now by posting problems and their solutions. ~Abhijit
7
5960
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# to set up the MSIOpenDatabase to open an existing MSI DB. I get a handle of 1. I'm not sure what I'm doing wrong. The RC is 0, so it thinks it's successful. Help Looking through the various SDK msi*.h files, I found that the value for the...
15
1668
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 Field Test2 (Alpha50) Table 3: Field IDnr (Number,key) and Field Test3 (Alpha50) 2. In the relation window link on IDnr: table 2 to table 1 (1x1) and table 3 to table 1 (1x1).
7
5883
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 testcases we experiance Segmentation fault from the python libraries. If i know how to handle the exception for Segmentation fault , it will help me complete the run on any testcase , even if i experiance Seg Fault due to any one or many functions in...
9
2333
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 generate an assertion failure for unoptimized, debug builds. We did this to find and fix cases where we were relying upon finalization rather than pro-active Dispose() calls. For classes that introduced IDisposable() into the class hierarchy...
64
3441
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 pointers. Can someone please explain why? thanks. Zytan
12
1895
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 something like this in PHP? Or do I need to find the php.ini file and look to see where the error
2
1331
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 back-up of the primary back-end on that front-end's local network; so that in the event the link is lost to the primary back-end, that location's will automatically connect to the back-up back-end. Confused yet, good me too. My delima is that when...
0
9704
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9571
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
10561
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10318
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...
0
10069
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7608
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6845
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();...
1
4277
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 we have to send another system
2
3803
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.