Mengatasi Insert Into Jadi Double In Asp.net

Mengatasi Double Insert Into In Asp.net

Avoid/Prevent/Stop Duplicate insert when page is refreshed or refresh button is clicked in browser in ASP.Net

jika anda mengalami insert into to database menjadi double alias duplicate,seperrti gini : ketika anda insert ke database kemudian data yang anda insert tersebut jadi double,maka ada 2 cara untuk mengatasinya,yaitu menambahkan Response.Redirect(Request.Url.AbsoluteUri); ataupun menambahkan Response.Redirect("~/Success.aspx")

Lihat implementasinya berikut ini dengan sesama :

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
 

This using the above simple technique you can easily stop or prevent duplicate inserts in your SQL Server Database in ASP.Net Application 

Begitulah caranya kawan.
selamat bercoding ria.

0 KOMENTAR