Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
When i execute this nothing happens
the datagridview does not update
C#
public void Search() 
{
   OleDbCommand ocm = new OleDbCommand("SELECT Firstname FROM students WHERE Firstname LIKE '% Hello %' ", conn);
   conn.Open(); 
   ocm.ExecuteNonQuery(); 
   conn.Close(); 
}
Posted
Updated 22-Mar-23 4:26am
v2

ExecuteNonQuery is designed to issue commands like INSERT, DELETE or UPDATE. It's not intended for use in SELECTs. What you could do, is use ExecuteDataReader like this:
C#
private List<string> names = new List<string>();
public void Search() 
{
  using (OleDbCommand ocm = new OleDbCommand("SELECT Firstname FROM students WHERE Firstname LIKE '% Hello %' ", conn))
  {
    conn.Open(); 
    OleDbDataReader reader = ocm.ExecuteDataReader(); 
    while (reader.Read())
    {
      names.Add(reader[0]);
    }
    conn.Close(); 
  }
}
 
Share this answer
 
Comments
Member 9430088 11-Jan-13 5:25am    
Sorry its still not working
You can use DataAdapter to get table from database :
C#
conn.Open();
OleDbDataAdapter ad = new OleDbDataAdapter("", conn);
DataTable dt = new DataTable();
ad.Fill(dt);
gridView.DataSource = dt;
 
Share this answer
 
v4
Comments
Member 9430088 11-Jan-13 5:25am    
sorry its still not working
Hi.You can refer to the following code.
C#
public void Search()
 {
   using (OleDbConnection con = new OleDbConnection(constring))
   {
      con.Open();
      OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT Firstname FROM students WHERE Firstname LIKE '% Hello %' ", con);
      DataTable tb = new DataTable();
      adapter.Fill(tb);
      dataGridView1.DataSource = tb;
   }
}


If you have any question,please contact me.
 
Share this answer
 
LINQ-To-DataSet (which i prefer):

var filtered = tb.AsEnumerable()
    .Where(r => r.Field<String>("CREATOR").Contains(searchstring));
ADO.NET(DataTable.Select):

DataRow[] filteredRows = tb.Select("CREATOR LIKE '%" + searchstring + "%'");
ADO.NET(DataView.RowFilter):

 tb.DefaultView.RowFilter = "CREATOR LIKE '%" + searchstring + "%'";
 
Share this answer
 
Comments
Dave Kreskowiak 22-Mar-23 13:53pm    
You didn't understand the question, from 10 years ago.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900