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
C#
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");
}
}
Sachin SinghRank #66 · 26.2K reputationPosted Nov 3, 2022, 4:09 AM EDT
Make sure your ID column is auto-incremented (Identity) in Sql table.
create table YourTableName {Id int primary key identity,--other columns }also make sure your Dnum column is varchar(50) as you are sending a string value and not int value.
Otherwise convert it to int
int Dnum = Convert.ToInt32(dno.SelectedItem.Value);Jaya PrakashRank #1,293 · 1.3K reputationPosted Nov 3, 2022, 4:39 AM EDT
i changed it but still the same error but error was at state in the db i take the varchar and here i take string still the same error
Amit MohantyRank #112 · 20K reputationPosted Nov 4, 2022, 6:32 AM EDT
Share your table structure.