Jaya Prakash

Jaya Prakash

  • 543
  • 2.3k
  • 65.2k

Crud Operations

Nov 3 2022 7:11 AM

When inserting the error is showing like this   "'Conversion failed when converting the varchar value 'AP' to data type int.'"

This My Code  (And Error is showing at ExecuteNonQuery()); 

Please Help me

protected void btn1_Click(object sender, EventArgs e)
{
    string firsName = fn.Text;
    string lName = ln.Text;
    string DeptName = dept.SelectedItem.Text;
    string Gender = string.Empty;
    if (m.Checked == true)
    {
        Gender = m.Text;
    }
    else
    {
        Gender = f.Text;
    }
    string hobby = string.Empty;
    if (h1.Checked == true)
    {
        hobby = h1.Text;
    }
    if (h2.Checked == true)
    {
        hobby = hobby+h2.Text;
    }
    if (h3.Checked == true)
    {
        hobby =hobby+h3.Text;
    }
    if (h4.Checked == true)
    {
        hobby =hobby+h4.Text;
    }
    string Dnum = dno.SelectedItem.Text;
    string State = stat.SelectedItem.Text;
    string City = ct.SelectedItem.Text;
    con.Open();
    string query = "Insert into emp values('" + firsName + "','" + lName + "','" + DeptName + "','" + Gender + "','" + hobby + "','" + Dnum + "','" + State + "','" + City + "')";
    SqlCommand cmd = new SqlCommand(query, con);
    int i = cmd.ExecuteNonQuery();
    con.Close();
    if (i == 1)
    {
        FillGrid();
        Response.Write("Emp Added");    
    }
}

 


Answers (3)