Duplicate records while inserting the data

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ebindia0041
    New Member
    • May 2007
    • 4

    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
  • Abdul Haque
    New Member
    • May 2007
    • 17

    #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.isPostba ck)
    {
    //insert related command
    }

    Comment

    • ebindia0041
      New Member
      • May 2007
      • 4

      #3
      Originally posted by Abdul Haque
      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..

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #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.

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          Originally posted by ebindia0041
          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

          Comment

          • ebindia0041
            New Member
            • May 2007
            • 4

            #6
            Originally posted by Plater
            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.

            Comment

            • Logan1337
              New Member
              • May 2007
              • 38

              #7
              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.

              Comment

              • Bheemsen Singh

                #8
                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:

                Code:
                void Page_Load(Object sender, EventArgs e)
                {
                            if(!IsPostBack)
                            {
                                Session["update"] =  Server.UrlEncode(System.DateTime.Now.ToString());
                            }
                }
                On the page's PreRender event, a ViewState variable is set to the value of the Session variable as follows:

                Code:
                    void Page_PreRender(object obj,EventArgs e)
                    {
                        ViewState["update"] = Session["update"];
                    }
                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:
                Code:
                    void btnSubmit_Click(object obj, EventArgs e)
                    {
                        string name = "";
                        string qualification = "";
                 
                 
                        if (Session["update"].ToString() == ViewState["update"].ToString())
                        {
                            if (txtName.Text != "" || txtName.Text != null)
                            {
                                name = txtName.Text.ToString();
                            }
                 
                            if (txtQualification.Text != "" || txtQualification.Text != null)
                            {
                                qualification = txtQualification.Text.ToString();
                            }
                 
                           //--- Insert data function should be execute here
                 
                           string strSql = "INSERT INTO Testdata (Name,Qualification) VALUES ('" + name + "','" + qualification + "')";
                           
                            SqlConnection ANConnection = new SqlConnection(ConnectionString);
                 
                            ANConnection.Open();
                            SqlCommand ANCommand = new SqlCommand(strSql, ANConnection);
                            ANCommand.ExecuteNonQuery();
                 
                            ANConnection.Close();
                            ANConnection.Dispose();
                                                   
                            //--End of save data
                           
                           lblMessage.Text = "Inserted Record Sucessfully
                           Session["update"] = Server.UrlEncode(System.DateTime.Now.ToString());
                        }
                        else
                        {
                            lblMessage.Text = "Failure – Due to Page Refresh";
                            txtName.Text = "";
                            txtQualification.Text = "";
                        }
                    }
                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.

                Comment

                Working...