0
votes
if (CheckBox1.Checked==false)
       {
           String strConnString = ConfigurationManager.ConnectionStrings["CallcenterConnectionString"].ConnectionString;
           SqlConnection con = new SqlConnection(strConnString);
           SqlCommand cmd = new SqlCommand("insert into CallCenter..Loy_DispMstr (CallType, SUBFormat, Disposition, SubDisposition) values (@CallType, @Format, @Disposition, @SubDisposition)", con);
           cmd.Parameters.AddWithValue("CallType" , SqlDbType.VarChar).Value = ddlCalltype.SelectedItem.Value;
           cmd.Parameters.AddWithValue("SUBFormat", SqlDbType.VarChar).Value = ddlFormat.SelectedItem.Value;
           cmd.Parameters.AddWithValue("Disposition", SqlDbType.VarChar).Value = ddlDisp.SelectedItem.Value;
           cmd.Parameters.AddWithValue("SubDisposition", SqlDbType.VarChar).Value = ddlSubdisp.SelectedItem.Value;
           con.Open();
           int i = cmd.ExecuteNonQuery();
           con.Close();  
           Label2.Text = " Your data is been saved in the database";
           Label2.ForeColor = System.Drawing.Color.ForestGreen;

       }
       else if(flag==0 && CheckBox1.Checked==true)
       {
           String strConnString = ConfigurationManager.ConnectionStrings["CallcenterConnectionString"].ConnectionString;
           SqlConnection con = new SqlConnection(strConnString);
           SqlCommand cmd = new SqlCommand("insert into CallCenter..Loy_DispMstr (CallType, SUBFormat,Disposition, SubDisposition) values (@CallType, @Format,@Disposition, @SubDisposition)", con);
           cmd.Parameters.AddWithValue("CallType", SqlDbType.VarChar).Value= ddlCalltype.Text;
           cmd.Parameters.AddWithValue("SUBFormat", SqlDbType.VarChar).Value= ddlFormat.Text;
           cmd.Parameters.AddWithValue("Disposition", SqlDbType.VarChar).Value= TextBox1.Text;
           cmd.Parameters.AddWithValue("SubDisposition", SqlDbType.VarChar).Value= TextBox2.Text;

           con.Open();
           int i = cmd.ExecuteNonQuery();//error on this
           con.Close();  


       }   
   }

the error is on the executenonquery the error is on the executenonquery

the error is on the executenonquery

the error is on the executenonquery

1
1. Change AddWithValue to Add. 2. add @ to the start of the parameters names. 3. You declared a parameter called @Format but added a parameter called SubFormat to your parameters collection. - Zohar Peled
@ZoharPeled Please do not provide answers as comments. There are several negative impacts that this behavior creates. - mickmackusa
@ZoharPeled now also error is coming - user31121992

1 Answers

0
votes

Try this it will work for you :

if (CheckBox1.Checked == false)
{
    String strConnString = ConfigurationManager.ConnectionStrings["CallcenterConnectionString"].ConnectionString;
    SqlConnection con = new SqlConnection(strConnString);
    SqlCommand cmd = new SqlCommand("insert into CallCenter..Loy_DispMstr (CallType, SUBFormat, Disposition, SubDisposition) values (@CallType, @Format, @Disposition, @SubDisposition)", con);
    cmd.Parameters.Add("@CallType", ddlCalltype.SelectedItem.Value);
    cmd.Parameters.Add("@Format", ddlFormat.SelectedItem.Value);
    cmd.Parameters.Add("@Disposition", ddlDisp.SelectedItem.Value);
    cmd.Parameters.Add("@SubDisposition", ddlSubdisp.SelectedItem.Value);
    con.Open();
    int i = cmd.ExecuteNonQuery();
    con.Close();
    Label2.Text = " Your data is been saved in the database";
    Label2.ForeColor = System.Drawing.Color.ForestGreen;

}
else if (flag == 0 && CheckBox1.Checked == true)
{
    String strConnString = ConfigurationManager.ConnectionStrings["CallcenterConnectionString"].ConnectionString;
    SqlConnection con = new SqlConnection(strConnString);
    SqlCommand cmd = new SqlCommand("insert into CallCenter..Loy_DispMstr (CallType, SUBFormat,Disposition, SubDisposition) values (@CallType, @Format,@Disposition, @SubDisposition)", con);
    cmd.Parameters.Add("@CallType", ddlCalltype.Text);
    cmd.Parameters.Add("@Format", ddlFormat.Text);
    cmd.Parameters.Add("@Disposition", TextBox1.Text);
    cmd.Parameters.Add("@SubDisposition", TextBox2.Text);

    con.Open();
    int i = cmd.ExecuteNonQuery();//error on this
    con.Close();
}