Avoid (Prevent) insert duplicate record in database using C# in ASP.Net

indradeo
 
on Oct 05, 2021 06:35 AM
474 Views

Thanks for suggestion.

code working but while i have insert single values then double value has been inserted.

please help me sir.

namespace TS
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Btnsave_Click(object sender, EventArgs e)
        {
            try
            {
                CreateConnection();
                OpenConnection();

                _sqlCommand.CommandText = "spx_TS5";
                _sqlCommand.CommandType = CommandType.StoredProcedure;
                _sqlCommand.Parameters.AddWithValue("@Dept ", TextBox2.Text.ToString());
                _sqlCommand.Parameters.AddWithValue("@wrk_nm ", TextBox3.Text.ToString());
                _sqlCommand.Parameters.AddWithValue("@fts_no", TextBox4.Text.ToString());
                _sqlCommand.Parameters.AddWithValue("@prced_by_dept_date", datepicker.Text.ToString());
                _sqlCommand.Parameters.AddWithValue("@estmt_cst ", TextBox6.Text.ToString());
                _sqlCommand.Parameters.AddWithValue("@prpsl_sts ", TextBox13.Text.ToString());
                _sqlCommand.Parameters.AddWithValue("@prsnt_lctn ", TextBox7.Text.ToString());
                _sqlCommand.Parameters.AddWithValue("@in_prss_sinc ", datepicker.Text.ToString());
                _sqlCommand.Parameters.AddWithValue("@Event", "Add");
                _sqlCommand.ExecuteNonQuery();

                int result = Convert.ToInt32(_sqlCommand.ExecuteNonQuery());

                if (result != 0)
                {
                    Response.Write("<script language='javascript'>window.alert('Record Is Inserted Successfully');window.location='Index.aspx';</script>");
                }
            }
            finally
            {
                CloseConnection();
                DisposeConnection();
            }
        }
    }
}

 

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Oct 05, 2021 06:39 AM
indradeo says:
_sqlCommand.ExecuteNonQuery();
 
                int result = Convert.ToInt32(_sqlCommand.ExecuteNonQuery());

You are executing the command two times. So remove the below line.

_sqlCommand.ExecuteNonQuery();