By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,847 Members | 2,354 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,847 IT Pros & Developers. It's quick & easy.

Duplicate records while inserting the data

P: 4
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
Share this Question
Share on Google+
7 Replies


P: 17
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

P: 4
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
Expert 5K+
P: 7,872
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
Expert Mod 5K+
P: 9,731
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

P: 4
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
P: 38
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

P: n/a
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

Post your reply

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