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);
}