Preventing Data Resend When Updating Page (F5) in ASP.NET

3

Well, I have the following problem:

I have a web application made with ASP.NET and C #.

In this application I have a simple system for registering items.

After I register an item in the application, if I have the browser page refreshed (press f5), the browser pops up asking to resend the form, consequently it generates a duplicate in the database.

This is the snippet of the code where I register at the bank:

PgSqlConnection Myconn = new PgSqlConnection(connectionString);
        query = "INSERT INTO banco(a,b,c,d,e,f) VALUES (" + a + "," + b.SelectedValue + "," + c + "," + d + "," + e +"," + f.SelectedValue + ")";
        PgSqlCommand Command = new PgSqlCommand(query, Myconn);
        Myconn.Open();
        try
        {
            Command.ExecuteNonQuery();
        }
        catch (PgSqlException ex)
        {
            String myscript = "alert('Não foi possível executar esta ação')";
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "myscript", myscript, true);
        }

How can I prevent this?

To facilitate future searches, here is the code snippet below with the solution to the problem:

    PgSqlConnection Myconn = new PgSqlConnection(connectionString);
    query = "INSERT INTO banco(a,b,c,d,e,f) VALUES (" + a + "," + b.SelectedValue + "," + c + "," + d + "," + e +"," + f.SelectedValue + ")";
    PgSqlCommand Command = new PgSqlCommand(query, Myconn);
    Myconn.Open();
    try
    {
        Command.ExecuteNonQuery();            
        Response.Redirect("estearquivo.aspx"); //isto soluciona o problema
    }
    catch (PgSqlException ex)
    {
        String myscript = "alert('Não foi possível executar esta ação')";
        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "myscript", myscript, true);
    }
    
asked by anonymous 26.03.2014 / 19:40

2 answers

7

I have already answered an equivalent question in PHP, but the same goes for any other language because what is in question is an HTTP request. Come on:

When you do a POST on a form, it is incorrect that you generate the listing (or a confirmation message, or anything else) in the POST response. It is certain that you redirect the user to another page, generating a GET soon after that POST.

With this, an F5 would cause a GET on the next page and not a POST, preventing the form from being posted again.

    
26.03.2014 / 20:00
4

This is a simple problem: when giving F5, since the last action was a POST, the browser resends the POST. As @Rodrigo Rigotti said you can in the first POST direct with a 301 to another page, for example send to the page of list of registered items.

The code you posted in your question is just the execution of the data insertion, it is not the one you need to change, but the control of HTTP requests. If you want to redirect to another page after entering the data, use the Response Redirect :

Response.Redirect("OutraPagina.aspx", false);

There is a detailed example this link .

Or you can save in the viewstate of your page the type of user action and verify the action before saving the data. See this another link with a detailed example .

I hope you have helped.

    
28.03.2014 / 14:19