New row inserted for each page refresh
Hi I'm getting a strange problem while inserting records into database. In my button click event I'm trying to insert some values i开发者_开发知识库nto my database it is working fine. Once insertion is completed... again if I press F5 or refresh the browser a new row is getting inserting with the previous values in the table.
Why it is happening?
Thank you
When you click the button, it sends a POST request to the server, and the updated page is sent back as the response. In order to refresh that page, the same POST must be made again, and the server interprets this as another button click.
Most Web browsers give a warning in this situation, saying that refreshing the page may repeat any action that was just performed (in your case, inserting a row in the database). But if you want to prevent this from happening at all, the best way is probably to respond to the POST request with a redirect. In ASP.NET with C#, the way to do this is:
Response.Redirect(url);
Redirecting back to the same page is fine, or you could also redirect to a different page. When the browser receives this redirect, it will issue a GET request for the specified page. Then if you refresh, no action will be taken.
you need to handle you page refresh event handler and check.
Simply because refresh caused another postback which re-do the insertion operation.
You might want to perform a redirect to the same page to prevent that. There are some other means to handle this issue as well.
It's happening because you're requesting the page again, using the same POST parameters that were used to request it the first time.
To fix it: either check for a page refresh in your code and don't do the insert in that event, or set a cookie the first time, and then don't do the insert again if the cookie is there. The cookie could either have a fixed duration (say 30 minutes), or it could be a session cookie, which would last until the users closes the browser.
Check the solution in the following link
http://aspalliance.com/687_Preventing_Duplicate_Record_Insertion_on_Page_Refresh.4
精彩评论