In this article I will explain with an example, how to prevent / avoid / stop duplicate (double) insertion of record on Page refresh in ASP.Net using C# and VB.Net.
Duplicate inserts or double updates occur when a Page is refreshed which causes Form re-submission and in turn causing re-execution of the last executed operation.
 
 
Problem
The problem is that when browser refresh button or F5 or CTRL + F5 function keys are pressed, the last executed event is again executed and a warning popup comes up which warns against Form resubmission.
For example, if you have a Form submission on Button click and after clicking button, Page is refreshed by the user then the Form is resubmitted and again same event gets called.
ASP.Net - Prevent Duplicate(Double) Inserts when Page is refreshed
 
 
Solution
Following are the two solutions to the above problem.
1. Redirection to other Page.
2. Redirection to same Page.
 
 
Examples
Below are the examples of the above two solutions. In the following example, an INSERT statement is executed and after that the necessary redirection is performed.
1. User is redirected to other page
Once the Database operation is completed i.e. INSERT or UPDATE, a redirection is performed to other Page.
Once the Page is redirected it is impossible for User to perform re-submission of the Form.
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
 
2. User is redirected to same page
Once the Database operation is completed i.e. INSERT or UPDATE, a redirection is performed to same Page.
Once the Page is redirected, it is impossible for User to perform re-submission of the Form.
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