473,847 Members | 1,714 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Duplicate records while inserting the data

4 New Member
This is like the bug from hell. It is kind of hard to explain, so
please bear with me.

Background Info: SQL Server 7.0, Asp.net 1.1 with c#

I'm inserting simple records into a table. But one insert command is
placing 2 or 3 records into the table. The 'extra' records, have the
same data as the previous insert incident, (except for the timestamp).

Here is an example. Follow the values of the 'Search String' field:

I inserted one record at a time, in the following order (And only one
insert per item):
airplane
jet
dog
cat
mouse
tiger

After this, I should have had 6 records in the table. But, I ended
up with 11!


Here is what was recorded in the database:

Vid DateTime Type ProductName SearchString NumResults
cgcgGeorgeWeb3 Fri Sep 26 09:48:26 PDT 2003 i null airplane 112
cgcgGeorgeWeb3 Fri Sep 26 09:49:37 PDT 2003 i null jet 52
cgcgGeorgeWeb3 Fri Sep 26 09:50:00 PDT 2003 i null dog 49
cgcgGeorgeWeb3 Fri Sep 26 09:50:00 PDT 2003 i null jet 52
cgcgGeorgeWeb3 Fri Sep 26 09:50:00 PDT 2003 i null jet 52
cgcgGeorgeWeb3 Fri Sep 26 09:50:22 PDT 2003 i null dog 49
cgcgGeorgeWeb3 Fri Sep 26 09:50:22 PDT 2003 i null cat 75
cgcgGeorgeWeb3 Fri Sep 26 09:52:53 PDT 2003 i null mouse 64
cgcgGeorgeWeb3 Fri Sep 26 09:53:06 PDT 2003 i null tiger 14
cgcgGeorgeWeb3 Fri Sep 26 09:53:06 PDT 2003 i null mouse 64
cgcgGeorgeWeb3 Fri Sep 26 09:53:06 PDT 2003 i null mouse 64

Look at the timestamps, and notice which ones are the same.

I did one insert for 'dog' , but notice how 2 'jet' records were
inserted
at the same time. Then, when I inserted the 'cat' record, another
'dog' record was inserted. I waited awhile, and inserted mouse, and
only the mouse was inserted. But soon after, I inserted 'tiger', and 2
more mouse records were inserted.

If I wait awhile between inserts, then no extra records are inserted.
( Notice 'airplane', and the first 'mouse' entries. ) But if I insert
records right after one another, then the second record insertion also
inserts a record with data from the 1st insertion.

i am simply using Query = "INSERT ... statement):
----------------------------------------------------------------------

I know that the function is not getting called multiple times
because I print out a message each time it is called.

This really stumps me. I'll really appreciate any help you can
offer.

Thanks,

ebindia0041
May 23 '07 #1
7 6662
Abdul Haque
17 New Member
please check whether the code of insert detail is present in page load or not!
in case if it is, then please use
if (!Page.isPostba ck)
{
//insert related command
}
May 23 '07 #2
ebindia0041
4 New Member
please check whether the code of insert detail is present in page load or not!
in case if it is, then please use
if (!Page.isPostba ck)
{
//insert related command
}
code of insert detail is present in the page and also using (!Page.IsPostba ck)
but it not work..
May 23 '07 #3
Plater
7,872 Recognized Expert Expert
Use the debugger and set a breakpoint on your insert calls (yes you can debug websites)

You could also try to set keys in your table to prevent duplicate entries. At least that way the extra inserts would fail.
ie ([col1], [col2]) should be unique

And make sure you use UPDATE and not INSERT if you are updating a field that already exists.
May 23 '07 #4
Frinavale
9,735 Recognized Expert Moderator Expert
code of insert detail is present in the page and also using (!Page.IsPostba ck)
but it not work..

Could you post the code that calls the update?

-Frinny
May 23 '07 #5
ebindia0041
4 New Member
Use the debugger and set a breakpoint on your insert calls (yes you can debug websites)

You could also try to set keys in your table to prevent duplicate entries. At least that way the extra inserts would fail.
ie ([col1], [col2]) should be unique

And make sure you use UPDATE and not INSERT if you are updating a field that already exists.

Thanks for replying..

I have to insert the data not update the data it will create error.
May 24 '07 #6
Logan1337
38 New Member
Are you using ADO.NET constructs such as DataSet and/or DataTable? If so, you might have a synchronization issue between these constructs and the database.

For example, if you add the items to the DataSet and then update the database, but forget to AcceptChanges() on the dataset, subsequent updates of the database might think the existing items in the table are new when they are in fact not.
May 24 '07 #7
Avoid Duplicate record insert on page refresh using ASP.NET

One of most common issue which many of the web developers face in their web applications, is that the duplicate records are inserted to the Database on page refresh. If the web page contains some text box and a button to submit the textbox data to the database. In that case when the user insert some data to the textbox and click on the submit button, it will save the record to the Database and then if the user refresh the web page immediately then the same record is again saved to the database as there is no unique keys that can be used to verify the existence of the data, so as to prevent the multiple insertion.

From this behavior we can definitely know that, on the page fresh the button click event is fired.
To avoid this problem we can try this method as discuss below.

On page load event save the date/time stamp in a session variable, when the page is first loaded, a Session variable is populated with the current date/time as follows:

Expand|Select|Wrap|Line Numbers
  1. void Page_Load(Object sender, EventArgs e)
  2. {
  3.             if(!IsPostBack)
  4.             {
  5.                 Session["update"] =  Server.UrlEncode(System.DateTime.Now.ToString());
  6.             }
  7. }
On the page's PreRender event, a ViewState variable is set to the value of the Session variable as follows:

Expand|Select|Wrap|Line Numbers
  1.     void Page_PreRender(object obj,EventArgs e)
  2.     {
  3.         ViewState["update"] = Session["update"];
  4.     }
Then these two values are compared to each other immediately before the database INSERT command is run.
If they are equal, then the command is permitted to execute and the Session variable is updated with the current date/time, otherwise the command is bypassed as given below:
Expand|Select|Wrap|Line Numbers
  1.     void btnSubmit_Click(object obj, EventArgs e)
  2.     {
  3.         string name = "";
  4.         string qualification = "";
  5.  
  6.  
  7.         if (Session["update"].ToString() == ViewState["update"].ToString())
  8.         {
  9.             if (txtName.Text != "" || txtName.Text != null)
  10.             {
  11.                 name = txtName.Text.ToString();
  12.             }
  13.  
  14.             if (txtQualification.Text != "" || txtQualification.Text != null)
  15.             {
  16.                 qualification = txtQualification.Text.ToString();
  17.             }
  18.  
  19.            //--- Insert data function should be execute here
  20.  
  21.            string strSql = "INSERT INTO Testdata (Name,Qualification) VALUES ('" + name + "','" + qualification + "')";
  22.  
  23.             SqlConnection ANConnection = new SqlConnection(ConnectionString);
  24.  
  25.             ANConnection.Open();
  26.             SqlCommand ANCommand = new SqlCommand(strSql, ANConnection);
  27.             ANCommand.ExecuteNonQuery();
  28.  
  29.             ANConnection.Close();
  30.             ANConnection.Dispose();
  31.  
  32.             //--End of save data
  33.  
  34.            lblMessage.Text = "Inserted Record Sucessfully
  35.            Session["update"] = Server.UrlEncode(System.DateTime.Now.ToString());
  36.         }
  37.         else
  38.         {
  39.             lblMessage.Text = "Failure – Due to Page Refresh";
  40.             txtName.Text = "";
  41.             txtQualification.Text = "";
  42.         }
  43.     }
  44.  
Note: that ViewState needs to be enabled on the page for this to work; if ViewState is not enabled then a hidden form field may be used instead.
Nov 18 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

2
13403
by: george | last post by:
This is like the bug from hell. It is kind of hard to explain, so please bear with me. Background Info: SQL Server 7.0, on an NT box, Active Server pages with Javascript, using ADO objects. I'm inserting simple records into a table. But one insert command is placing 2 or 3 records into the table. The 'extra' records, have the same data as the previous insert incident, (except for the timestamp).
2
5001
by: ms | last post by:
Access 2000: I am trying to delete duplicate records imported to a staging table leaving one of the duplicates to be imported into the live table. A unique record is based on a composite key of 3 fields (vehicleID, BattID, and ChgHrs). VehicleID and BattID are a TEXT datatype and ChrHrs are a number(long int.) datatype. Since records to be imported can have duplicate records of the composite key I need to clean all but one of the...
6
8212
by: sara | last post by:
I have a procedure to automate bringing several Excel files into our Access tables, on a daily basis. The problem is that if the user has a problem, and tries to run the import again (maybe 3 files imported then there was a data problem and they want to re-import after fixing the problem), I can't get the Error handling to fire if the user is attempting to import duplicate key records. Message when I try to import records already on...
6
6531
by: planetthoughtful | last post by:
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...
8
2702
by: mindwarp | last post by:
Hi, When a user submits / posts data my php script Inserts data into my database. If they refresh the script or click back and click submit again I get duplicate record. Is there an easy solution to this? Or do I need to create a hidden field which contains some sort of
4
5873
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 and is "unique". when I run
6
12013
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) 1.) Is an "Cannot insert duplicate key row" exception from a FormView returned as part of the ItemInserting or ItemInserted event? 2.) What is the recommended way to catch and deal with an "Cannot
2
1576
by: mtgriffiths86 | last post by:
Hi All, What i am trying to do is avoid inserting duplicate records into a database. I am inserting the records using a webpage but when i enter a flight number(primary key) that already exists i get an error and system crashes. What i want to do is if a duplicate record is about to be entered then redirect the user to another page. Is this possible Thanks in advance Matthew
4
4205
by: Thomas Arthur Seidel | last post by:
Hello to all, I have a small or big problem with a customer data base, where during a change of system we might have created duplicate records. This should be easy to find, you might think, but, we are talking about roughly 10000 records or less in a total volume of 1 MIO records or more. I have considered a strategy: The station ID and a field with something like a sequence number are supposed to be unique during that period. The...
0
9730
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
10983
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...
1
10706
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
10338
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
7882
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
7056
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
5719
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
5911
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3164
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.