Clicking Browser refresh is causing records to be inserted multiple times

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nateraaaa
    Recognized Expert Contributor
    • May 2007
    • 664

    Clicking Browser refresh is causing records to be inserted multiple times

    I have an admin application that allows the user to enter information and click a Save button. In the click event of the save button I call an insert stored proc. If the insert proc is successful I then display a javascript pop up telling the user that the record has been added successfully. I then show a datagrid to the user with the record that they just added. My problem is that if I click refresh after successfully adding a record another duplicate record is added. This occurs everytime that I click the browser refresh button. I have tried creating a bool method to determine if the record already exists in the datagrid that I display to the user. This did not work for me. Has anyone else encountered this problem? How can I fix this?

    Nathan
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Are you using the isPostBack boolean to determine if someone has clicked a button or just hit refresh?

    Comment

    • TRScheel
      Recognized Expert Contributor
      • Apr 2007
      • 638

      #3
      Originally posted by nateraaaa
      I have an admin application that allows the user to enter information and click a Save button. In the click event of the save button I call an insert stored proc. If the insert proc is successful I then display a javascript pop up telling the user that the record has been added successfully. I then show a datagrid to the user with the record that they just added. My problem is that if I click refresh after successfully adding a record another duplicate record is added. This occurs everytime that I click the browser refresh button. I have tried creating a bool method to determine if the record already exists in the datagrid that I display to the user. This did not work for me. Has anyone else encountered this problem? How can I fix this?

      Nathan
      A bool method? Why not a bool session variable? Pseudocode:

      if((bool)Sessio n["IsAdded"))
      {
      Session["IsAdded"] = true;
      AddRecord()
      }
      ....

      If(readyToAddNe wRecord())
      Session["IsAdded"] = false;

      Comment

      • nateraaaa
        Recognized Expert Contributor
        • May 2007
        • 664

        #4
        Thank you for the suggestions. I ended up calling a select stored procedure that passed in the username as a parameter. When the browser refresh is clicked the code will run the select stored proc by inserting the username that was entered prior to the page refresh. If no record is found with that username the insert proc will continue; if the user is found the proc will be skipped and I display a message to the user telling them that the user already exists in the database table.

        Code:
        string branch = txtNewUserBranch.Text;
        string IP = txtUserIP.Text;
        string[] IParray = IP.Split(new char[] {'.'});
        string networkid = IParray[0].ToString() + "." + IParray[1].ToString() + "." + IParray[2].ToString();
        string octet4 = IParray[3].ToString();
        
        if(!drsql.BrowseData(networkid, Convert.ToInt32(octet4),this.txtNewUser.Text , ref ds))
        {
        lblMessage.Visible = true;
        lblMessage.ForeColor = Color.Red;
        lblMessage.Text = "There was a problem loading the data";
        }
        if(ds.Tables[0].Rows.Count == 0)
        {
        if(drsql.InsertNewUser(networkid, octet4, octet4, this.txtNewUser.Text.ToString(), branch.ToUpper()))
        {
        lblMessage.Visible = true;
        lblMessage.Text = "<script>alert('New User Added Successfully!');</script>";
        this.txtNewUser.Text = "";
        this.txtUserIP.Text = "";
        this.txtNewUserBranch.Text = "";
        pnlUser.Visible = false;
        BindData();					
        return;				
        }
        else
        {						
        lblMessage.Visible = true;
        lblMessage.ForeColor = Color.Red;
        lblMessage.Text = this.txtNewUser.Text + " has already been added to the database";
        pnlUser.Visible = false;
        this.txtNewUser.Text = "";
        this.txtUserIP.Text = "";
        this.txtNewUserBranch.Text = "";
        return;			
        }
        Nathan
        Last edited by nateraaaa; Jul 19 '07, 09:31 PM. Reason: Added code tags around code example

        Comment

        • menocomp
          New Member
          • Apr 2007
          • 5

          #5
          write:

          response.redire ct("The same page")

          after the save button run the stored procedure so that the refresh button doesnot add the same record twice.

          Comment

          Working...