preventing duplicate data entry on RELOAD

Discussion in 'ASP .Net' started by Darrel, Dec 5, 2004.

  1. Darrel

    Darrel Guest

    I have a web form that inserts a record into a DB. This function is called
    from the button_click subroutine.

    After the record is inserted, I show/hide some different panels and let the
    page postback.

    The problem is that if a person reloads this page after submitting, a new
    record is inserted into the DB.

    Short of doing an actual compare query prior to entering the new record, or
    redirecting to a new page, is there a way to prevent this from happening?

    -Darrel
     
    Darrel, Dec 5, 2004
    #1
    1. Advertising

  2. Hi Darrel,

    one way to avoid this is to clear all values in the cache after postback.
    You can use the Javascript doPostBack function for the same. Place a label
    control "lblPostback" at the end of the page. Next place the following code
    in the respective events as mentioned below:

    Page Load Event:

    If Page.IsPostback Then
    lblPostback.Text = ""
    End If

    Button Submit Event:
    On the button submit event, after all the processing has been done, place
    the following code snippet-

    lblPostback.Text = "<script>javascript:__doPostBack('','');</script>"

    HTH.

    Kaustav Neogy.

    "Darrel" wrote:

    > I have a web form that inserts a record into a DB. This function is called
    > from the button_click subroutine.
    >
    > After the record is inserted, I show/hide some different panels and let the
    > page postback.
    >
    > The problem is that if a person reloads this page after submitting, a new
    > record is inserted into the DB.
    >
    > Short of doing an actual compare query prior to entering the new record, or
    > redirecting to a new page, is there a way to prevent this from happening?
    >
    > -Darrel
    >
    >
    >
     
    =?Utf-8?B?S2F1c3Rhdg==?=, Dec 6, 2004
    #2
    1. Advertising

  3. Kaustav's suggestion will work, but it will cause an extra trip to the
    server. You could also use the Page's RegisterStartupScript method to invoke
    a Javascript method at the Page's load.

    One thing you should ask yourself is, would it be possible to redirect the
    user to a different page (using Response.Redirect), and achieve the same
    results? For example, if you have a Form that has a set of TextBoxs and a
    button which will insert the data, you can simply redirect the user to the
    same page, which will get rid of any PostBack issues (such as having the
    non-user-friendly "The page cannot be refreshed..." confirm box IE has).
    Perhaps you could use a QueryString that would tell the page to use
    information from the newest submitted record. I'm not sure of your
    situation.

    Hope I could help,
    Johann MacDonagh

    "Kaustav" <> wrote in message
    news:...
    > Hi Darrel,
    >
    > one way to avoid this is to clear all values in the cache after postback.
    > You can use the Javascript doPostBack function for the same. Place a label
    > control "lblPostback" at the end of the page. Next place the following
    > code
    > in the respective events as mentioned below:
    >
    > Page Load Event:
    >
    > If Page.IsPostback Then
    > lblPostback.Text = ""
    > End If
    >
    > Button Submit Event:
    > On the button submit event, after all the processing has been done, place
    > the following code snippet-
    >
    > lblPostback.Text = "<script>javascript:__doPostBack('','');</script>"
    >
    > HTH.
    >
    > Kaustav Neogy.
    >
    > "Darrel" wrote:
    >
    >> I have a web form that inserts a record into a DB. This function is
    >> called
    >> from the button_click subroutine.
    >>
    >> After the record is inserted, I show/hide some different panels and let
    >> the
    >> page postback.
    >>
    >> The problem is that if a person reloads this page after submitting, a new
    >> record is inserted into the DB.
    >>
    >> Short of doing an actual compare query prior to entering the new record,
    >> or
    >> redirecting to a new page, is there a way to prevent this from happening?
    >>
    >> -Darrel
    >>
    >>
    >>
     
    Johann MacDonagh, Dec 6, 2004
    #3
  4. Darrel

    Darrel Guest

    > I'm not sure of your situation.

    I have a page with two panels. One panel is the form. One panel is the
    'success' confirmation.

    On completion of a succesful submit, I hid the form panel and show the
    success panel.

    When a person reloads the page, though, it resubmits the data. I was
    confused because the data is only submitted via the button_click
    function...not the page_load function.

    However, I think simply adding a flag on submit is the way to go...but yea,
    it looks like a roundtrip to the server.

    I suppose redirectly to another page is perhaps a better idea. However, I
    have dozens of these submit/confirm combo pages so that may have to get put
    off and added to the bottom of the to-do list.

    -Darrel
     
    Darrel, Dec 6, 2004
    #4
  5. Darrel

    Hans Kesting Guest

    Darrel wrote:
    >> I'm not sure of your situation.

    >
    > I have a page with two panels. One panel is the form. One panel is the
    > 'success' confirmation.
    >
    > On completion of a succesful submit, I hid the form panel and show the
    > success panel.
    >
    > When a person reloads the page, though, it resubmits the data. I was
    > confused because the data is only submitted via the button_click
    > function...not the page_load function.
    >


    "reload" means "execute the same action that originally gave you this page",
    so the browser will act (submit the form) as if that button was pressed,
    using the values that were previously on the form. So a resubmit of the
    same data is correct (though unwanted :) ) behaviour.

    > However, I think simply adding a flag on submit is the way to
    > go...but yea, it looks like a roundtrip to the server.
    >


    But: it will use the *previous* values!

    > I suppose redirectly to another page is perhaps a better idea.
    > However, I have dozens of these submit/confirm combo pages so that
    > may have to get put off and added to the bottom of the to-do list.
    >
    > -Darrel
     
    Hans Kesting, Dec 6, 2004
    #5
  6. Darrel

    darrel Guest

    > > However, I think simply adding a flag on submit is the way to
    > > go...but yea, it looks like a roundtrip to the server.
    > >

    >
    > But: it will use the *previous* values!


    Ah...duh. You're right. Hmm...crap...this seems like something .net should
    be able to handle for you ;o)

    I guess it looks like a final page redirect and/or checking for duplicate
    records in the DB are the only answer? (Though the javascript solution might
    work...though I'd rather avoid javascript for checkig data integrity).

    -Darrel
     
    darrel, Dec 6, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    497
    Jerry III
    Aug 11, 2003
  2. Roedy Green
    Replies:
    4
    Views:
    467
  3. =?Utf-8?B?SGF3aw==?=
    Replies:
    0
    Views:
    522
    =?Utf-8?B?SGF3aw==?=
    Feb 21, 2007
  4. Oleg Konovalov

    Preventing duplicate form submission

    Oleg Konovalov, Oct 31, 2006, in forum: Java
    Replies:
    14
    Views:
    1,378
  5. Mike Owen

    Allowing entry of a Carriage Return during data entry

    Mike Owen, Jul 27, 2006, in forum: ASP .Net Web Controls
    Replies:
    3
    Views:
    796
    Alessandro Zifiglio
    Jul 27, 2006
Loading...

Share This Page