I have a fairly simple web app that is used to let people interact with a local database. The embedded SQL is throwing the following error:
System.Data.SqlClient.SqlException: 'Incorrect syntax near '<'.'
This only happens when the text box is empty. If it contains data everything works as it should. I would like for the query to essentially ignore the field if it is blank.
protected void findBTN_Click(object sender, EventArgs e)
{
using (SqlConnection sqlCon = new SqlConnection(connString))
{
sqlCon.Open();
string query = "SELECT * FROM[OEM_Ref] WHERE DrawID like CASE WHEN " + drawerIDbox.Text + " <> '' THEN '" + drawerIDbox.Text + "%' ELSE '%' End";
SqlDataAdapter sqlDa = new SqlDataAdapter(query , sqlCon);
DataTable dtbl = new DataTable();
sqlDa.Fill(dtbl);
GridView1.DataSource = dtbl;
GridView1.DataBind();
}
}