tags:

views:

172

answers:

2

Hi I've a simple code to insert data into db in button click event. It is executing fine and insert data into db. After inserting data if i press f5 button it is again inserting a new row into db with the same values.

Button click event fires on each f5 button (refresh)

Wat is the problem?

Thank you, Nagu

+2  A: 

This is as designed. When you press F5 you are redoing what you just did which was a postback. To prevent this you have an endless amount of solutions such as:

Use a session variable on initial load and once it is posted the first time (saved), clear the value. Make sure you are checking for the presence of this value.

// On inital page load for data entry store a key.
// This is a basic example, you should have a session wrapper
Session["Unsaved"] = 1;

// When you do the save logic make sure you check this session variable and save if
// it still exists.
if (Session["Unsaved"] != null)
{
  // Save data here
  Session.Remove("Unsaved");
}
else
{
  // Show message that save has already completed or session has expired.
}

You could also do the check DB side and verify that you are not inserting identical data (which may not work as it could be valid in your scenario)

In the past I have prevented this with edits for example by storing a revision number and having it submit the revision number with the posted data. When saving, if the revision numbers don't match (since it is changed with each update and the check is done DB side) then the update fails and I would display a message.

For new data, you could also do a Response.Redirect to a 'complete' page which gives them updated information of success / failure. If they hit refresh, they will just be reloading the completed page and not the page that was doing the insert / updating.

Again there are so many different solutions. Nothing is perfect for every scenario so you will need to find out which works best for you.

Kelsey
Hi thank you for your quick response, and is this the only solution to handle this problem? or there any better approach?
Nagu
F5 is a browser command that tells the browser to refresh the page. In this case, it means to resubmit your postback. If you override this behavior, you will break standard browser functionality that users expect. This is a big no-no in the UI design world.
David Lively
+1  A: 

One approach you can take is to redirect the web browser after the postback. This will make it so that hitting the refresh button or F5 will cause the new page to load, instead.

Example:

protected void button_Click(object sender, EventArgs args)
{
    // Insert data, etc.

    Response.AddHeader("Location", "DifferentPage.aspx");
}
Jacob
or Response.Redirect back to the same page. Hitting F5 will just GET the page again, and not rePOST the postback.
russau
Redirecting to the same page usually won't allow you to give some type of confirmation / message of success or failure with out special coding looking for something so typically a new page is used to keep this functionality seperated.
Kelsey