Mohit Kapoor

Mohit Kapoor

  • 1.6k
  • 94
  • 19.9k

Search from DB with LIKE Operator

Aug 28 2016 4:46 PM
 I am getting an incorrect syntax error on following code .
There's an error in Sql query ?
 
protected void btnByName_Click(object sender, EventArgs e)
        {
            string name = txtByName.Text.Trim();

            Response.Write("clicked \n");
            cmd.Connection = con;
          
            cmd.CommandText = "select * from books where name LIKE '%'  "+name+"  '%'  ";
            
            con.Open();
            rdr=cmd.ExecuteReader();
          
          if(rdr.Read())
            {
               Response.Write(" found <br>");
                    GridView1.DataSource = rdr;
                    GridView1.DataBind();
            }
            else
                Response.Write("no match found");
             con.Close();

            

        }

Answers (4)