473,320 Members | 1,612 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,320 software developers and data experts.

Duplicate records while inserting the data

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 6584
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.isPostback)
{
//insert related command
}
May 23 '07 #2
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.isPostback)
{
//insert related command
}
code of insert detail is present in the page and also using (!Page.IsPostback)
but it not work..
May 23 '07 #3
Plater
7,872 Expert 4TB
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 Expert Mod 8TB
code of insert detail is present in the page and also using (!Page.IsPostback)
but it not work..

Could you post the code that calls the update?

-Frinny
May 23 '07 #5
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
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
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. ...
2
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...
6
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...
6
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...
8
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...
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...
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) ...
2
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...
4
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,...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
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...
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: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.