First things first- your code is susceptible to
SQL Injection.
NEVER EVER
concatenate SQL commands and variables to form a query.
The recommended way to use variables and commands is to use the
SqlParameter[
^] class to add your variables to the Command object.
Applied to your code will give us something similar to this
private void button2_Click(object sender, EventArgs e)
{
string Tx1 = textbox1.Text;
string Tx2 = textbox2.Text;
string Tx3 = textbox3.Text;
string CB1 = comboBox1.Text;
con.Open();
string qry = "INSERT INTO Employee_Table VALUES (@Tx1, @Tx2, @Tx3, @CB1)";
SqlCommand cmd = new SqlCommand(qry, con);
cmd.Parameters.AddWithValue("@Tx1", Tx1);
cmd.Parameters.AddWithValue("@Tx2", Tx2);
cmd.Parameters.AddWithValue("@Tx3", Tx3);
cmd.Parameters.AddWithValue("@CB1", CB1);
cmd.ExecuteNonQuery();
MessageBox.Show("Inserted Data Successfully");
con.Close();
}
So now that we got that taken care of, lets look at your error
System.Data.SqlClient.SqlException
'Conversion failed when converting the varchar value 'string' to data type int.'
This is pretty self explanatory- You are trying to place a string value into a database field that is defined to only accept integers.
Which one of these values is the problem really cannot be determined because we do not know what the table structure is, and there is nothing intuitive from the code you have provided.
What I would do is set a
breakpoint on immediately after the 4 variables are populated at the opening of the script, and run this in debug mode. When you get to that point, check to see what the values that are assigned to your text- and check- boxes; and compare that to the data-types within the database.
According to the error- one of these is supposed to be an integer. Then all you need to do is change the variable declaration to reflect an Int instead of a String.
Reference:
SqlParameter Class (System.Data.SqlClient) | Microsoft Docs[
^]