Prevent Duplicate (Double) Insert when Page is refreshed in ASP.Net

akhter
 
on Nov 20, 2021 04:47 AM
585 Views

When i click on reload this page on browser, it is inserting last inserted record in database again

namespace UsedClothing
{
    public partial class grnitem : System.Web.UI.Page
    {
        protected void btn_Update_Click(object sender, EventArgs e)
        {
            if (txtdate.Text == "")
            {
                Response.Write("<script>alert('Please Select  Date ')</script>");
                txtdate.Focus();
            }
            if (txtremarks.Text == "")
            {
                Response.Write("<script>alert('Please Input Remarks Of Date Change. ')</script>");
                txtremarks.Focus();
            }
            else
            {
                string query = "SP_Inert_GRN";
                using (SqlCommand cmd = new SqlCommand(query))
                {
                    cmd.Connection = con;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@Action", "Updatedate");
                    cmd.Parameters.AddWithValue("@GRN_ID", lbid.Text);

                    cmd.Parameters.AddWithValue("@GRN_Date", txtdate.Text);
                    cmd.Parameters.AddWithValue("@U_Remarks", txtremarks.Text);

                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                    lbmsgu.Text = "Update Date Successfully";
                    txtremarks.Text = "";
                    txtdate.Text = "";
                }
            }
        }

        protected void btn_Uitem_Click(object sender, EventArgs e)
        {
            if(lbid.Text == "")
            {
                Response.Write("<script>alert('Please Select Grn')</script>");
            }
            else if(txtweight.Text == "")
            {
                Response.Write("<script>alert('Please Input Weight')</script>");
                txtweight.Focus();
            }
            else if (ddlitem.SelectedValue == "0")
            {
                Response.Write("<script>alert('Please Select Item')</script>");
            }
            else if (txtremksitem.Text == "")
            {
                Response.Write("<script>alert('Please Input Remarks')</script>");
                txtremksitem.Focus();
            }
            else
            {
                using (SqlCommand cmd = new SqlCommand("[SP_Inert_GRN]", con))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@Action", "Updateitem");
                    cmd.Parameters.AddWithValue("@D_ID", lbidd.Text.Trim());
                    cmd.Parameters.AddWithValue("@Item_Code", ddlitem.SelectedValue);
                    cmd.Parameters.AddWithValue("@Item_Weight", txtweight.Text);
                    cmd.Parameters.AddWithValue("@U_Remarks", txtremksitem.Text);
                    cmd.Parameters.AddWithValue("@Item_U_Date", DateTime.Now.ToString());
                    cmd.Parameters.AddWithValue("@Item_U_Time", DateTime.Now.ToString("hh:mm:ss"));
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                    lbmsg.Text = "Udpdate Successfully";
                    ddlitem.SelectedValue = "0";
                    txtweight.Text = "";
                    lbidd.Text = "";

                    Loadgridview();
                }
            }
        }      
    }
}

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
arjunv
 
on Nov 20, 2021 04:49 AM

Hi Akhter,

 Please refer below Article.

ASP.Net - Prevent Duplicate(Double) Inserts when Page is refreshed

 

akhter
 
on Nov 20, 2021 07:00 AM

but how to display successfull message then redirect