Prevent Duplicate record insertion on page refresh in asp.net

Problem:
Today I have faced a new problem(new for me), when I press save button it works fine and then refresh(F5 or Refresh button on browser) the page but this time it is execute the button click event again. For this duplicate data posting on the server.

I try to find out the problem, nothing wrong in my code. Now turn to google and figure out the problem with solution. I have got several solutions. Now I would like to share this solution which solved my problem ......

Solution:

Assume that in Test.aspx page you have a button and a TextBox:
<asp:Button ID="btnSubmit" runat="server" Text="Submit" Width="110px" OnClick="btnSubmit_Click" />
<asp:TextBox ID="TxtName" runat="server"></asp:TextBox>

In Test.aspx.cs..:
public partial class Test : System.Web.UI.Page
{
protected override void OnInit(EventArgs e)
{
btnSubmit.Click += new EventHandler(btnSubmit_Click);base.OnInit(e);
}
protected void Page_Load(object sender, EventArgs e)
{

}
private void Save_Click(object sender, EventArgs e)
{
if (CheckIfDuplicateRequest())

{
return;

}
//Your code to insert the value of the SomeText textbox into the database.
//Set the value of one of the key fields in your web form in session
Session["Refresh"] = TxtName.Text;

}

private Boolean CheckIfDuplicateRequest()
{
if (Session["Refresh"] == null)
{
//Probably submitting the page for the first time
return false;
}
String previousValue = Session["Refresh"].ToString();
if (previousValue != TxtName.Text)
{
//Submitting the page with a different set of values
return false;
}
//Duplicate request.
return true;
}

}

you can get more help from these post:
http://aspalliance.com/687
http://forums.asp.net/p/1296011/3702515.aspx
http://forums.asp.net/p/1190997/2045619.aspx

Comments

Anonymous said…
I can suggest to come on a site on which there is a lot of information on this question.
Anonymous said…
what if the user just leaves away, and we waste the server memory for session variable, don't we?
Anonymous said…
the previous comment is mine, if you have had a beautiful way to deal with that problem, please share with me. thank you.
unmeaningly at gmail dot com
Anonymous said…
you could just use response.redirect to the same page; if refresh is then pressed all it does is reload the page and not touch the database...

i wouldn't suggest using session as it uses resources on the server, maybe better, if you dont want to response.redirect to put the value in a querystring:

www.mysite.com?value_entered=confirmed

then check for the value before entering info into the database...

if request.querystring(value_entered) = "confirmed" then etc

Popular posts from this blog

The model backing the 'MyDBContext' context has changed since the database was created. Either manually delete/update the database, or call Database.SetInitializer with an IDatabaseInitializer instance. For example, the DropCreateDatabaseIfModelChanges strategy will automatically delete and recreate the database, and optionally seed it with new data.

How can I get a value of a property from an anonymous type (C#)

Check/Uncheck all items in a CheckBoxList using Javascript