I am getting this error : “Error 102 : non-boolean type specified in a context where a condition is expected” for this request : [closed]

Posted on

Question :

private void button2_Click(object sender, EventArgs e)
{
    con.Open();
    SqlCommand cmd = con.CreateCommand();
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = "delete from MYDB where First name='" + first.Text + "'";
    cmd.ExecuteNonQuery();
    con.Close();
    disp_data();
    MessageBox.Show("Contact deleted successfully");
}

Answer :

You can solve it by putting First name between brackets:

delete from MYDB where [First name]='" + first.Text + "'";
create table t (id int, [First name] varchar(100));
insert into t values (1, 'John Doe');
GO
delete from t where First name = 'John Doe';
GO
Msg 4145 Level 15 State 1 Line 1
An expression of non-boolean type specified in a context where a condition is expected, near 'name'.

db<>fiddle here

Leave a Reply

Your email address will not be published. Required fields are marked *