C# Question

what is the proper query for insert and select statement with Like clause?

i have this query and it gives me error which is unavaible to insert null on table tbl_emp_gs. i don't know what is wrong with my code here.

con.Open();
sqc = con.CreateCommand();
string query1 = "INSERT INTO tbl_emp_gs (EmployeeID,StepID) VALUES ('"+ lblEmpID.Text +"', (Select StepID from tbl_gradestep where StepID Like @grade))";
sqc.Parameters.AddWithValue("@grade",cmbStepNumber.Text);
sqc.CommandText = query1;
sdr = sqc.ExecuteReader();
MessageBox.Show("Data Saved.", "Message",MessageBoxButtons.OK,MessageBoxIcon.Information);


i want to get the step ID from tbl_gradestep table,

Answer

The Inner query is returning NULL. You should follow like syntax in order to retrieve a correct result (If it exists) :

string query1 = "INSERT INTO tbl_emp_gs (EmployeeID,StepID) VALUES ('"+ lblEmpID.Text +"', (Select StepID from tbl_gradestep where StepID Like @grade))";

sqc.Parameters.AddWithValue("@grade", "%" + cmbStepNumber.Text + "%");

I suggest using sqc.ExecuteNonQuery() unless you have a valid reason to execute a data reader.

Also, it is not recommended to concat strings in SQL statements.