The program should output from the database a string whose value in the Author field fully or partially coincided with the value entered in the SearchTBR SearchTBR . It gives an error: can not read sql query (9 line).

 if (Convert.ToString(SearchTBR.Text) != "") { SqlConnection cn = new SqlConnection("data source = .\\mysql; initial catalog = Library; integrated security = true"); SqlCommand cmd = new SqlCommand(); SqlDataReader dr; cn.Open(); cmd.CommandText = "select BookID, Author, Title, YEAR(Year), Category, Availability from Bibliography where (Author LIKE '% & SearchTBR.Text & %')"; cmd.Connection = cn; dr = cmd.ExecuteReader(); while (dr.Read()) { ListViewItem lv = new ListViewItem(dr[0].ToString()); lv.SubItems.Add(dr[1].ToString()); lv.SubItems.Add(dr[2].ToString()); lv.SubItems.Add(dr[3].ToString()); lv.SubItems.Add(dr[4].ToString()); lv.SubItems.Add(dr[5].ToString()); listViewLLReader.Items.Add(lv); } cn.Close(); } 

    1 answer 1

    Right here

     cmd.CommandText = "select BookID, Author, Title, YEAR(Year), Category, Availability from Bibliography where (Author LIKE '% & SearchTBR.Text & %')"; 

    I understand they meant

     cmd.CommandText = "select BookID, Author, Title, YEAR(Year), Category, Availability from Bibliography where (Author LIKE '%"+SearchTBR.Text+"%')";