1

When I'm searching my datagrid, it should be always the first word to type. If i type the next word or consecutive letter it contains, it doesn't show.

Example: I'm going to search Welcome Back. If i type "Elcome" or "back", it doesn't show.

Here is my code...

OleDbCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Select * From books where Author like ('" + textBox1.Text + "%')";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
da.Fill(dt);
dataGridView1.DataSource = dt;
Hamid Pourjam
  • 20,441
  • 9
  • 58
  • 74
Dannehkown
  • 59
  • 1
  • 9

1 Answers1

3

It is because you are telling the database to select rows that starts with textBox1.Text followed by anything else (%) you should change it to

"Select * From books where Author like ('%" + textBox1.Text + "%')";

this will find all books that their authors have textBox1.Text somewhere in their value.

In like command % can be replaced by zero or more character.

Also try using parameterized queries, your code is vulnerable to SQL injection.

Community
  • 1
  • 1
Hamid Pourjam
  • 20,441
  • 9
  • 58
  • 74