sql server - An expression of non-boolean type specified in a context where a condition is expected, near 'NAME'.' -
my code:
string sqlselectquery = " select * [kts managment] staff name=" + convert.tostring(textbox1.text); sqlcommand cmd = new sqlcommand(sqlselectquery, con); sqldatareader dr = cmd.executereader();
i error:
an expression of non-boolean type specified in context condition expected, near 'name'
you should always use parametrized queries avoid sql injection - still #1 vulnerability in computing.
thus, code should this:
string connectionstring = "......"; // typically read config file string query = "select * [kts managment] staff name = @name"; using (sqlconnection con = new sqlconnection(connectionstring)) using (sqlcommand cmd = new sqlcommand(query, con) { cmd.parameters.add("@name", sqldbtype.varchar, 100).value = textbox1.text; con.open(); using (sqldatareader dr = cmd.executereader()) { // read values sql data reader.... } con.close(); }
this approach avoid error have missing and/or mismatched single or double quotes around strings in sql statement ...
Comments
Post a Comment