471,338 Members | 1,022 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,338 software developers and data experts.

Clicking Browser refresh is causing records to be inserted multiple times

nateraaaa
663 Expert 512MB
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
Jul 19 '07 #1
4 3157
Plater
7,872 Expert 4TB
Are you using the isPostBack boolean to determine if someone has clicked a button or just hit refresh?
Jul 19 '07 #2
TRScheel
638 Expert 512MB
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)Session["IsAdded"))
{
Session["IsAdded"] = true;
AddRecord()
}
....

If(readyToAddNewRecord())
Session["IsAdded"] = false;
Jul 19 '07 #3
nateraaaa
663 Expert 512MB
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.

Expand|Select|Wrap|Line Numbers
  1. string branch = txtNewUserBranch.Text;
  2. string IP = txtUserIP.Text;
  3. string[] IParray = IP.Split(new char[] {'.'});
  4. string networkid = IParray[0].ToString() + "." + IParray[1].ToString() + "." + IParray[2].ToString();
  5. string octet4 = IParray[3].ToString();
  6.  
  7. if(!drsql.BrowseData(networkid, Convert.ToInt32(octet4),this.txtNewUser.Text , ref ds))
  8. {
  9. lblMessage.Visible = true;
  10. lblMessage.ForeColor = Color.Red;
  11. lblMessage.Text = "There was a problem loading the data";
  12. }
  13. if(ds.Tables[0].Rows.Count == 0)
  14. {
  15. if(drsql.InsertNewUser(networkid, octet4, octet4, this.txtNewUser.Text.ToString(), branch.ToUpper()))
  16. {
  17. lblMessage.Visible = true;
  18. lblMessage.Text = "<script>alert('New User Added Successfully!');</script>";
  19. this.txtNewUser.Text = "";
  20. this.txtUserIP.Text = "";
  21. this.txtNewUserBranch.Text = "";
  22. pnlUser.Visible = false;
  23. BindData();                    
  24. return;                
  25. }
  26. else
  27. {                        
  28. lblMessage.Visible = true;
  29. lblMessage.ForeColor = Color.Red;
  30. lblMessage.Text = this.txtNewUser.Text + " has already been added to the database";
  31. pnlUser.Visible = false;
  32. this.txtNewUser.Text = "";
  33. this.txtUserIP.Text = "";
  34. this.txtNewUserBranch.Text = "";
  35. return;            
  36. }
Nathan
Jul 19 '07 #4
write:

response.redirect("The same page")

after the save button run the stored procedure so that the refresh button doesnot add the same record twice.
Jul 25 '07 #5

Post your reply

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

Similar topics

reply views Thread by Alan Johns | last post: by
5 posts views Thread by Andrew Chanter | last post: by
10 posts views Thread by jaYPee | last post: by
12 posts views Thread by shank | last post: by
9 posts views Thread by Mr Newbie | last post: by
reply views Thread by rosydwin | last post: by

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.