|
|
How to Avoid Duplicate record insertion on page refresh(F5) in ASP.NET using C#
|
|
|
|
|
Just after record insertion or update, redirect to the same page.
MyData.Insert();
Server.Transfer("SamePage.aspx");
Hope this will solve your problem.
|
|
|
|
|
Thanks for your replay.But it is not working.
after submit i want to display message "that data has successfully inserted".but when i redirect the page it not show.
|
|
|
|
|
To display your message, you can use querystring
like:
string Msg = "The data has successfully inserted";
Server.Transfer("SamePage.aspx?msg=" + Msg);
Then, read the querystring and display the message if any.
if(!string.IsNullOrEmpty(Request.QueryString["msg"]))
DisplayMessage();
There should be some better way also but at this time, this is what is hitting in my mind.
|
|
|
|
|
Put this in the page load
if (ispostback)<br />
{<br />
return;<br />
}
|
|
|
|
|
You could check your database to see if a record with all the information has already been inserted.
|
|
|
|
|
I dont have details of your requirement.One way also might work for you, just have a flag in viewstate at your page,set it true once inserted and check on every record insertion.
Or Simply you can have check at your DB side so that it wont insert the same record more than one time..Cheers!!
Brij
|
|
|
|
|
For insertion you can validate at your database(and you should).
But If you update your record once, then press F5, the same record will be updated twice.
There is no harm, but why to update twice...
Using ViewState might also not work for Updating data because user may update the same record after next postback.
I hope you are understanding what i am trying to say.
|
|
|
|
|
I understood your point.. Thats what I said it should be done keeping in mind the requirements of the Page.
Anyways thanks for pointing it out.Cheers!!
Brij
|
|
|
|
|
There is no straight forward way to avoid this without refreshing the page. The simplest way to go is to use Server.Transfer or Response.Redirect from the server after completing the save.
Since you wish to display a message then you can do it in either of the two ways.
1. Simply put a button to refresh the data above the data section and hope that users use this instead of the browsers' refresh button [F5]. In most cases, the users would use this instead of F5. If its a intranet application then you can even educate your users on the same. You could also put a message next to the button telling them to use this rather than F5.
2. After you complete the save and give the message, just push a client script that would refresh the page using say "window.self.location=" after waiting for a specified time say 5 seconds. That way, your message would be visible for 5 seconds and the postback would be cleared.
|
|
|
|
|
i appreciate your solution but there is some problem in your solution.
1)i cant forced any user to use only refresh button.user can click any button.
2)second option is seen good but it is not a appropriate solution of my problem.
i want to display message when submit button will press,and when user click on refresh(F5) then message will we clean.
|
|
|
|
|
How about clearing the fields/variables after insertion? Then before inserting you validate that data to make sure it exists.
|
|
|
|
|
I invested some time investigating this and the only solution I found working is based on some interaction between client data (ViewState, ...) and server data (Session, ...). Why:
- I don't accept solutions like "Check if the record already exists in the database" etc., because this is limited to inserting data to database, useless for any other scenario. Plus it has other flaws.
- You can't rely solely on client data (by this I mean data received from the client, like ViewState, POST data etc.), because when the user hits F5, the same request with the exact same data is sent to the server - if you accept the request as genuine the first time (and insert some data to the database), there is no way to dismiss it the second time (after refresh) because the data is exactly the same, so any deterministic algorithm for refresh detection can't possibly work. Half the people in the posts above don't realize this.
- I don't accept page redirections, because they either don't work (see above, try yourself if you don't believe) or if you try hard and make them work, they destroy the page state so it may be difficult to keep track of where the user was in the application, etc.
This eliminates all the solutions from the above posts. Like I said, the only way I got to work was to maintain a state-interaction between some Session variable (or any server piece of data) and ViewState variable (or any client piece of data). During normal requests, you keep these in sync and when you receive some Insert request, you compare them and check if they are in sync. If they are, the request is genuine and you can perform the insert. If the user hits Refresh (F5), the ViewState data is old compared to the server data and you recognize the request as refresh and do not perform the insert.
I event made a small component for this purpose, which you simply add to the page and then call BlahBlah.IsRefresh whenever you need. I can publish it somewhere if anyone cares. I think Microsoft should have long ago addded this functionality to the Framework so we could call Page.IsRefresh just like Page.IsPostback.modified on Wednesday, February 17, 2010 3:56 AM
|
|
|
|
|
I am developing a mail application. Through smtp I am sending mail.
I work properly. Mail will send successfully.
After sending mail I am display message your mail has send successfully(which is show after the mail has been send).
But the problem is that when I press F5 button then mail again send.
And it is sending again and again when ever I press F5
I donot want to send mail again when pressing F5 and message will also will display after mail has been send.
I tray to redirect the page it solve my problem to resend the page but in this situation massage will not display.
Plz can u help me
|
|
|
|
|
Im having around 30 Checkboxes in page i need to insert name of those checkboxes in a single column(seperated by comma) of table into db and also need to bind them from db
how can i do it in simple way....
|
|
|
|
|
I dont think there is simple way except making a temp collection after getting data from db and again costomise the names according to your requirement. Cheers!!
Brij
|
|
|
|
|
I think you can use a CheckBoxList for this. Just get the items from the database, split it on "," and get a array/collection and bind it in..
BTW, why would you want to save the names of checkboxes to the db?? It should be a pre-defined list editable through the DB or other means only.
|
|
|
|
|
in CS page split it with ',' and using for loop bind the each checkbox. now when you want to save in DB again using loop, save in one string (comma separated)(both Id and Text), pass this string to DB and using while loop split it with comman and update the text with particular Id.
let me know if you have any issues.Regards
Keyur Satyadev
|
|
|
|
|
Hi All,
I am deploying my website by using add webdeployment project.
But in my website i have used the WebUserControls in my website. The WebUserControls events are handled in form[which contains WebUserControl] through Delegates & Event Handlers.
These events of WebUserControl have been initialized at form Default.aspx Page Init event.
When i build the Project it gives no errors, the site runs perfectly...
But when build the Deployment project i got a compilation errors of WebUserControl events where i initilized at form Default.aspx page init event.
Its giving error message like 'MyUserControlUC' could not found are u missing add reference.
For sample:
==========
MyUserControlUC.ascx
<pre>
<scrpt runat="server">
//delegate
public delegate void DropDownSelectedIndexChanged(object sender, EventsArgs e);
//event
public event DropDownSelectedIndexChanged IndexChanged;
//call the event
protected void ddl_items_SelectedIndex_Changed(object sender, EventsArgs e)
{
if(IndexChanged != null)
IndexChanged(sender,e);
}
<asp:dropdownlist id="ddl_items" runat="server" autopostback="true" onselectedindexchanged="ddl_items_SelectedIndex_Changed">
|
Default.aspx
<%@ Register TagPrefix="uc1" TagName="SampleUserControl" Src="~/MyUserControlUC.ascx" %>
<html>
<head></head>
<body>
<form runat="server">
<uc1:SampleUserControl BackColor="Gray" id="SampleUserControl1" runat="server"></uc1:SampleUserControl>
</form>
</body>
</html>
Default.aspx.cs
<pre> protected override void OnInit(EventArgs e)
{
base.OnInit(e);
InitializeComponent();
}
private void InitializeComponent()
{
this.Load += new EventHandler(this.Page_Load);
SampleUserControl1.IndexChanged += new ASP.MyUserControlUC.SelectedIndexChanged(ddl_items_SelectedIndexChanged);//Here i am getting error while building deployment project.
}
private void ddl_items_SelectionIndexChanged(Object sender, EventArgs e)
{
//Code here to handle ...
}
Thanks & Regardsmodified on Tuesday, February 16, 2010 8:06 AM
|
|
|
|
|
If you are having the UCs in different project from the main web site, then check if the Build order of the solution is set correctly and check if you have enabled compiling the primary assembly and all it dependencies.
HTH!
|
|
|
|
|
Hi Dinesh,
Thanks for ur reply.
But the WebUserControl are in sample project.i.e, in main website it self
Regards,
kv sekhar
|
|
|
|
|
mm, try qualifying the UC with it relative path like so -
<%@ Register TagPrefix="uc1" TagName="SampleUserControl" Src="~/MyUserControlUC.ascx" %><br />
This denotes taht "MyUserControlUC" is available in the application root folder.
|
|
|
|
|
Hi Dinesh,
<%@ Register TagPrefix="uc1" TagName="SampleUserControl" Src="~/MyUserControlUC.ascx" %>
is right, in the post i wrote wrong. In register tag no error.
Problem with at Initilization of UserControl events in form.
any suggestions is appreciated..
Thanks & regards,
kv sekhar
|
|
|
|
|
Can you post the exact compilation message and the reference line number?
Well, I can't think of whats going wrong. Hard luck.modified on Tuesday, February 16, 2010 5:21 AM
|
|
|
|