In this article I will explain with an example, how to avoid the following message in browser when browser refresh button or CTRL + F5 is clicked after form is submitted i.e. PostBack in ASP.Net.
To display the website again the web browser needs to resend the information you’ve previously submitted. If you are making purchase, you should click Cancel to avoid a duplicate transaction. Otherwise click retry to display the webpage again.
The problem is that when browser refresh button or F5 or CTRL + F5 function keys are pressed, the last executed event is again executed.
ASP.Net: To display the website again the web browser needs to resend the information
The simplest resolution to solve this problem is redirecting the user after execution of insert or update operation. You can achieve the same in two ways
Redirect to some another page
After insert or update statement is executed you can redirect the user to some other page as described below
C#
private void InsertData()
{
    string strQuery = "insert into customers (CustomerID, CompanyName) values(@CustomerID, @CompanyName)";
    SqlCommand cmd = new SqlCommand(strQuery);
    cmd.Parameters.AddWithValue("@CustomerID", "A234");
    cmd.Parameters.AddWithValue("@CompanyName", "DCB");
    InsertUpdateData(cmd);
    Response.Redirect("~/Success.aspx");
}
 
VB.Net
Private Sub InsertData()
    Dim strQuery As String = "insert into customers (CustomerID, CompanyName) values(@CustomerID, @CompanyName)"
    Dim cmd As SqlCommand = New SqlCommand(strQuery)
    cmd.Parameters.AddWithValue("@CustomerID", "A234")
    cmd.Parameters.AddWithValue("@CompanyName", "DCB")
    InsertUpdateData(cmd)
    Response.Redirect("~/Success.aspx")
End Sub
 
Redirect to same page
After insert or update statement is executed you can redirect the user to the same page as described below
C#
private void InsertData()
{
    string strQuery = "insert into customers (CustomerID, CompanyName) values(@CustomerID, @CompanyName)";
    SqlCommand cmd = new SqlCommand(strQuery);
    cmd.Parameters.AddWithValue("@CustomerID", "A234");
    cmd.Parameters.AddWithValue("@CompanyName", "DCB");
    InsertUpdateData(cmd);
    Response.Redirect(Request.Url.AbsoluteUri);
}
 
VB.Net
Private Sub InsertData()
    Dim strQuery As String = "insert into customers (CustomerID, CompanyName) values(@CustomerID, @CompanyName)"
    Dim cmd As SqlCommand = New SqlCommand(strQuery)
    cmd.Parameters.AddWithValue("@CustomerID", "A234")
    cmd.Parameters.AddWithValue("@CompanyName", "DCB")
    InsertUpdateData(cmd)
    Response.Redirect(Request.Url.AbsoluteUri)
End Sub
 
Thus using the above simple technique you can easily stop or prevent duplicate inserts in your SQL Server Database in ASP.Net Application