Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Avoid Duplicate record insertion on page refresh(F5)

Avoid Duplicate record insertion on page refresh(F5)

Scheduled Pinned Locked Moved ASP.NET
csharpasp-nettutorial
14 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A amitamit099

    How to Avoid Duplicate record insertion on page refresh(F5) in ASP.NET using C#

    A Offline
    A Offline
    Anurag Gandhi
    wrote on last edited by
    #2

    Just after record insertion or update, redirect to the same page.

    MyData.Insert();
    Server.Transfer("SamePage.aspx");

    Hope this will solve your problem.

    Anurag Gandhi.
    http://www.gandhisoft.com
    Life is a computer program and every one is the programmer of his own life.

    A 1 Reply Last reply
    0
    • A amitamit099

      How to Avoid Duplicate record insertion on page refresh(F5) in ASP.NET using C#

      J Offline
      J Offline
      JustWorking
      wrote on last edited by
      #3

      Put this in the page load if (ispostback) { return; }

      1 Reply Last reply
      0
      • A amitamit099

        How to Avoid Duplicate record insertion on page refresh(F5) in ASP.NET using C#

        R Offline
        R Offline
        Rhys Jacob
        wrote on last edited by
        #4

        You could check your database to see if a record with all the information has already been inserted.

        1 Reply Last reply
        0
        • A amitamit099

          How to Avoid Duplicate record insertion on page refresh(F5) in ASP.NET using C#

          B Offline
          B Offline
          Brij
          wrote on last edited by
          #5

          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

          A 1 Reply Last reply
          0
          • B Brij

            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

            A Offline
            A Offline
            Anurag Gandhi
            wrote on last edited by
            #6

            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.

            Anurag Gandhi.
            http://www.gandhisoft.com
            Life is a computer program and every one is the programmer of his own life.

            B 1 Reply Last reply
            0
            • A Anurag Gandhi

              Just after record insertion or update, redirect to the same page.

              MyData.Insert();
              Server.Transfer("SamePage.aspx");

              Hope this will solve your problem.

              Anurag Gandhi.
              http://www.gandhisoft.com
              Life is a computer program and every one is the programmer of his own life.

              A Offline
              A Offline
              amitamit099
              wrote on last edited by
              #7

              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.

              A 1 Reply Last reply
              0
              • A Anurag Gandhi

                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.

                Anurag Gandhi.
                http://www.gandhisoft.com
                Life is a computer program and every one is the programmer of his own life.

                B Offline
                B Offline
                Brij
                wrote on last edited by
                #8

                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

                1 Reply Last reply
                0
                • A amitamit099

                  How to Avoid Duplicate record insertion on page refresh(F5) in ASP.NET using C#

                  D Offline
                  D Offline
                  Dinesh Mani
                  wrote on last edited by
                  #9

                  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.

                  A 1 Reply Last reply
                  0
                  • A amitamit099

                    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.

                    A Offline
                    A Offline
                    Anurag Gandhi
                    wrote on last edited by
                    #10

                    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.

                    Anurag Gandhi.
                    http://www.gandhisoft.com
                    Life is a computer program and every one is the programmer of his own life.

                    1 Reply Last reply
                    0
                    • D Dinesh Mani

                      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.

                      A Offline
                      A Offline
                      amitamit099
                      wrote on last edited by
                      #11

                      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.

                      1 Reply Last reply
                      0
                      • A amitamit099

                        How to Avoid Duplicate record insertion on page refresh(F5) in ASP.NET using C#

                        B Offline
                        B Offline
                        bgates1970
                        wrote on last edited by
                        #12

                        How about clearing the fields/variables after insertion? Then before inserting you validate that data to make sure it exists.

                        1 Reply Last reply
                        0
                        • A amitamit099

                          How to Avoid Duplicate record insertion on page refresh(F5) in ASP.NET using C#

                          M Offline
                          M Offline
                          Member 1033907
                          wrote on last edited by
                          #13

                          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

                          A 1 Reply Last reply
                          0
                          • M Member 1033907

                            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

                            A Offline
                            A Offline
                            amitamit099
                            wrote on last edited by
                            #14

                            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

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups