473,473 Members | 1,511 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Avoid inserting a duplicate record on page submit?

Hi All,

I have a C# ASP.NET page that submits back to itself to insert details
from a form into a database table. When / if the user refreshes the
page (and gets the standard warning that POST data will be
resubmitted), the previously submitted record is sumbitted again, and a
duplicate record is inserted into the table.

In PHP I would have avoided this by submitting the form to a processing
page, which would then automatically redirect back to the form page to
display success, etc. So, it would look something like this in PHP:

dataentry.php --> processdataentry.php --> dataentry.php

Doing this would avoid a duplicate record being inserted if
dataentry.php was refreshed.

I'm wondering how the same issue is handled in C# in ASP.NET? Do you
essentially do the same thing? ie submit the form to a processing .aspx
file and then redirect back to the dataentry page? Or is there a way of
avoiding duplicate insertions (aside from setting up unique indexes on
the underlying table - unfortunately, duplicates *are* at times
legitimate, so I can't assume that the existence of any duplicate means
that the page was incorrectly refreshed) while still posting back to
the same page?

Any help appreciated!!

Much warmth,

planetthoughtful
---
"Lost in thought"
http://www.planetthoughtful.org

May 29 '06 #1
6 6497
As if I haven't been in enough trouble lately, let me assert that the best
way to keep from having a duplicated row in your database is by the use of a
proper natural primary key.

You can join in on the fight by reading my blog at
http://www.dalepreston.com/Blog/2005...ty-crisis.html.

HTH,

Dale
--
Dale Preston
MCAD C#
MCSE, MCDBA
"planetthoughtful" wrote:
Hi All,

I have a C# ASP.NET page that submits back to itself to insert details
from a form into a database table. When / if the user refreshes the
page (and gets the standard warning that POST data will be
resubmitted), the previously submitted record is sumbitted again, and a
duplicate record is inserted into the table.

In PHP I would have avoided this by submitting the form to a processing
page, which would then automatically redirect back to the form page to
display success, etc. So, it would look something like this in PHP:

dataentry.php --> processdataentry.php --> dataentry.php

Doing this would avoid a duplicate record being inserted if
dataentry.php was refreshed.

I'm wondering how the same issue is handled in C# in ASP.NET? Do you
essentially do the same thing? ie submit the form to a processing .aspx
file and then redirect back to the dataentry page? Or is there a way of
avoiding duplicate insertions (aside from setting up unique indexes on
the underlying table - unfortunately, duplicates *are* at times
legitimate, so I can't assume that the existence of any duplicate means
that the page was incorrectly refreshed) while still posting back to
the same page?

Any help appreciated!!

Much warmth,

planetthoughtful
---
"Lost in thought"
http://www.planetthoughtful.org

May 29 '06 #2

Dale wrote:
As if I haven't been in enough trouble lately, let me assert that the best
way to keep from having a duplicated row in your database is by the use of a
proper natural primary key.


[snippage]
I'm wondering how the same issue is handled in C# in ASP.NET? Do you
essentially do the same thing? ie submit the form to a processing .aspx
file and then redirect back to the dataentry page? Or is there a way of
avoiding duplicate insertions (aside from setting up unique indexes on
the underlying table - unfortunately, duplicates *are* at times
legitimate, so I can't assume that the existence of any duplicate means
that the page was incorrectly refreshed) while still posting back to
the same page?


Hi Dale,

As per my original post, I can't use an index to suppress duplicates,
as duplicates might be the result of legitimate data entry. I'm trying
to remove or minimize the chances of accidental duplicates through
refreshing the page. As I said, in PHP I would have simply passed the
form to a processing page, and redirected back. I was (and still am)
curious to know if C# has an in-built mechanism to deal with this, or
if the same strategy I've used in PHP is the best way to go to achieve
what I'm looking for.

Much warmth,

planetthoughtful
---
"Lost in thought"
http://www.planetthoughtful.org

May 29 '06 #3
If, in the normal course of business, you add duplicate data to the table,
how do you know which to retrieve later?
--
Dale Preston
MCAD C#
MCSE, MCDBA
"planetthoughtful" wrote:

Dale wrote:
As if I haven't been in enough trouble lately, let me assert that the best
way to keep from having a duplicated row in your database is by the use of a
proper natural primary key.


[snippage]
I'm wondering how the same issue is handled in C# in ASP.NET? Do you
essentially do the same thing? ie submit the form to a processing .aspx
file and then redirect back to the dataentry page? Or is there a way of
avoiding duplicate insertions (aside from setting up unique indexes on
the underlying table - unfortunately, duplicates *are* at times
legitimate, so I can't assume that the existence of any duplicate means
that the page was incorrectly refreshed) while still posting back to
the same page?


Hi Dale,

As per my original post, I can't use an index to suppress duplicates,
as duplicates might be the result of legitimate data entry. I'm trying
to remove or minimize the chances of accidental duplicates through
refreshing the page. As I said, in PHP I would have simply passed the
form to a processing page, and redirected back. I was (and still am)
curious to know if C# has an in-built mechanism to deal with this, or
if the same strategy I've used in PHP is the best way to go to achieve
what I'm looking for.

Much warmth,

planetthoughtful
---
"Lost in thought"
http://www.planetthoughtful.org

May 29 '06 #4
I Agree 100% with Dale.
It's a common knowledge to add a primary key in your database table; by
chance perhaps even unique constraints for certain non-key columns.
A good practice is to implement a procedure that inserts records and
the procedure should be inside a SQL TRY CATCH that returns back a user
friendly error that the data already exists.
You cannot rely aalone on the asp.net interface to validate your data.
But even though u don't want to touch that, cosider the Postback in
ASP.NET.

May 29 '06 #5

Dale wrote:
If, in the normal course of business, you add duplicate data to the table,
how do you know which to retrieve later?


Hi Dale,

Lol, I'm surprised this has become such a difficult issue, and that
no-one seems to want to touch my original question, despite it being
extremely legitimate in web design.

Oh well. Cough. Hmmm. Imagine, if you will, a simple web app used to
record activity. Imagine then, that you may perform the same action on
the same task several times. Imagine also that you as a developer have
been asked to build an interface that allows 'type-click-click-submit'
functionality (one text box, two list boxes and a submit button) and,
very importantly, that a user might legitimately record exactly the
same information several times in a row.

Now, imagine that all that's really going to be used out of this is a
count of records per activity, per task, per user id, per day.

Now, just for the sake of the argument, and without running off on a
'unique key! unique key!' tangent, how would you minimize the
opportunity for accidental insertion of duplicate records by a page
refresh?

I've already explained how I would achieve this in PHP. You will see
the "entrypage.php --> processpage.php --> entrypage.php" technique
used a lot on sites for inserting blog comments, for example - where a
page refresh might otherwise insert a duplicate comment, but where
multiple comments by the same person is very normal.

I agree, in principle, that a unique key would have been an ideal
solution, if it were applicable. But, sadly, unless someone can see
something I'm missing, it's not.

Much warmth,

planetthoughtful
---
"Lost in thought"
http://www.planetthoughtful.org

May 30 '06 #6
dataentry.php --> processdataentry.php --> dataentry.php
It can be done in the same matter using asp.net using
Response.Redirect("PageName.aspx");
Hence,
dataentry.aspx -> processdataentry.aspx -> dataentry.aspx.
The Response.Redirect Method can be implemented inside an event of
button vlick for instance or as u wish.
Again, I have faced similar issues of clients requesting me to prevent
the insertion of duplicate records; they also wanted me to NOT touch
the existing database; i did that for them and still emphasized that it
is a POOR design and alternative and a proper one is to rethink the
originla design of the database.
Validation wise, I recommened a double layer way; one on the database
side and one in the interface side.
Yu do not want the clients to get uggly database errors and exceptions
if they attempt to insert duplicate records even though the database
prevents it, then u create in your asp.net page user-friendly
validations as a sub of the primary database validations.

Best regards

May 30 '06 #7

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

Similar topics

4
by: luc wastiaux | last post by:
Hello, I am writing an application to handle support requests, and the user needs to have his request number printed out in front of him even before he hits the "submit" button. I have no idea how...
24
by: el_roachmeister | last post by:
Is there a way to make a text link post to a form without passing all the parameters in the url? The urls tend to get very long and messy. I often wonder if there is a limit to how long they can...
2
by: Brian Lin | last post by:
Hi all, I have a table called Category and it has only two fields: CategoryID and CategoryName. I want user to be able to enter 10 categories at one page. So I what I do right now is to place 10...
4
by: FangQ | last post by:
hi I am very new to mysql. I have a question about using the "on duplicate update" clause with insert command. my table "data" has two columns, field1 and field2, where field1 is the index...
10
by: Clamato | last post by:
Good Morning, I'm working with a form that basically add's a users windows logon ID, first name, and last name to a table. A list box on this form is then requeried once added displaying the...
10
by: jmartmem | last post by:
Greetings, I have an ASP page with a 5x5 table embedded inside an Insert Record Form. This table contains several fields (mostly drop down list menus) and is used for corporate timekeeping (users...
2
by: AlexanderDeLarge | last post by:
Hi! I got a problem that's driving me crazy and I'm desperately in need of help. I'll explain my scenario: I'm doing a database driven site for a band, I got these tables for their discography...
2
by: hakkatil | last post by:
Hi to all, I have a page that inserts excel sheet to access database. I am using asp. What I want to do is to check the inserting record if it is in the database. Basicly checking the dublicate...
1
by: javediq143 | last post by:
Hi All, This is my first post in this forum. I'm developing a CMS for my latest website. This CMS is also in PhP & MySQL. I'm done with the ADD section where the Admin can INSERT new records in...
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
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
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
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...
0
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...
1
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
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
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
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.