Hi I'm getting a strange problem while inserting records into database.
In my button click event I'm trying to insert some values into 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
Related
I'm using .NET c#.
I have a page for search which displays results on next page.
If you select a Course and that's not the one you want,you can click back IN BROWSER and go to search results to select another.
But clicking back in browser causes expired page.
I have a back button which works fine but clicking back in browser expirse the data.
How do i fix this?
The data on the expired page are sent by POST method. It will require user to re-send the data. What you can do is change form method to GET (not always acceptable due to data size, security, url readability, logging and other factors). If GET is not an option, you should cache the POST result somehow (for example, putting it in a session) and make a redirect to a clean page which will take and display the cached result without requiring user to re-send it. When you return via Back button to that page, there will be no problem because that page is not generated directly by POST request, it only shows the results.
If the POST result is too big to be cached, then simply cache the input parameters and build a query based on them in the "clean" page. Once it has displayed the result, the Back button will work without the need to reload.
Im implementing Web Form using ASP.NET in c#, I have a page with few TextBox and a Submit Button, the data from TextBox going to insert into Database when Submit Button is pressed, the page will be retain and no redirect is required.
However, there is a issue after the data has been submitted, if F5/refresh the page without click on the Submit Button, the data will insert again and again.
How can I avoid such issue happen after the first submission?
Thank you in advanced.
Place Response.Redirect to the same page after save logic done. This happens because browser repeat last request on F5 in you case this is POST request to save. After Response.Redirect last request will be GET and you problem will be solved
Unfortunately you can't prevent something like that because the browser excute the same code again after you press refresh so thier is nothing you can do there but you can use response.redirect to the same page again.
Also you can check for duplication in the database so if that record exist don't save it again.
Hope I Helped
I have a user registration page, after entering all the details and successful account creation, I want to stay in the same page, but all the user entered details should be cleared
but if the registration is unsuccessful then all the user entered details should be retained.
which is the right way of achieving this?
is clearing all the values manually in code, the only way?
Viewstate is maintained during postbacks. So, you can do a redirect to the same url if the registration is successful. Make sure that your databindings check for IsPostback.
The state for the textboxes is maintained during postback, so you could clear them manually.
However Ingenu's answer suggest to use whats called the PRG-pattern which is an even better fit. In your case it goes like this:
On succesfull creation of an account, you should issue a redirect to the same page. The redirect will call your registration page as a GET request again, and clear all textboxes.
If the create is unsuccessfull, you should NOT redirect. So that the user stays on the same page with the textboxes still filled.
I have a user complaining about frequent timeouts in my Intranet web page. While looking for a solution I found this post:
http://forums.asp.net/t/152925.aspx?PageIndex=1
Where a poster recommends intercepting the redirect to the login page, submit the data to the database, then either reauthorize the user (without their knowledge) or redirect to login page. My goal is to avoid the situation where a user enters data in a form, walks away, then comes back to submit it, only to be told they have to login again (which is fine, if the data remained and the user was sent right back to the original webform).
Does anyone know how I can accomplish this on specific pages in my app (not all of them)?
It's not necessarily trivial, but you can add an ajax component that makes occasional calls to a page to keep the session alive. This way you could lengthen the session for any particular page you need to without affecting the application as a whole.
EDIT
If you really want to let the session expire, but keep the form data, you can implement
protected void Application_PostAuthenticateRequest (object sender, EventArgs e)
event handler in your global.asax.cs file. This is called before the forms authentication redirect happens, and the form data is available to your application at this point, so you can persist it to whatever medium is necessary until your user is authenticated again. In addition, if you check the
((HttpApplication)sender).Request.Path
property it will tell you which page was requested.
Well, the easy way it to drastically lengthen the timeout specified in the web.config file.
I'm going to try using cookies to preserve the data. My plan is to update the user's cookie after each control is changed, then add logic to the page_load property of the page to populate the form data after the user is logged back in.
I have a view that shows a list of items and when you click on one of them it will take you to the items page. I also store a "Viewed" flag in the database for that item.
Now, when the user clicks back, the item should be styled differently because of the change in the "Viewed" flag. However, every time I click back, it is as it was before I clicked into the item, and I have to click refresh to see the actual state of the page now.
How can I prevent this page from being cached so when a user clicks back they will see the latest version of this site, complete with the new styling?
Mark the controller action that generates the list with the OutputCacheAttribute and set the cache location to none to prevent that page from being cached on the client. This should cause the client to request the page again. If the user is using the back button, however, I think that the page is served up by the browser without reloading regardless of the caching. At least in FF I don't see it requesting the page again using Firebug.
[OutputCache( Location = OutputCacheLocation.None )]
Call this in your controller action:
Response.Cache.SetCacheability(HttpCacheability.NoCache)
This will prevent the browser from caching the page.