Hello and good day to all. I have a little question about like statement of sql/c#, I can’t figure out why my code doesn’t work.
if (Combo_List.Text == "What")
{
listView1.Items.Clear();
myds.Clear();
mydaEvents.SelectCommand = myconn.CreateCommand();
mydaEvents.SelectCommand.CommandText = "select * from Eventstbl where What like '@what%'";
mydaEvents.SelectCommand.CommandType = CommandType.Text;
mydaEvents.SelectCommand.Parameters.Add("@what", SqlDbType.NVarChar, 2000, "What").Value = text_Search.Text;
mydaEvents.Fill(myds, "Eventstbl");
foreach (DataRow item in myds.Tables["Eventstbl"].Rows)
{
ListViewItem items = new ListViewItem(item["EventsID"].ToString());
items.SubItems.Add(item["What"].ToString());
listView1.Items.Add(items);
}
}
The @what% won’t work but when I put ‘a%’ all items that begin in letter a shows in my listview1. I don’t know how to fix this issue. help me please. Thank you in advance.
Try this:
Text inside quotes will be treated as…text – you want your parameter to be interpreted as its string value, so you have to move it out.