Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0 Access OleDb
When i execute this nothing happens
the datagridview does not update
public void Search() 
{
   OleDbCommand ocm = new OleDbCommand("SELECT Firstname FROM students WHERE Firstname LIKE '% Hello %' ", conn);
   conn.Open(); 
   ocm.ExecuteNonQuery(); 
   conn.Close(); 
}
Posted 10-Jan-13 22:16pm
Edited 11-Jan-13 5:09am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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:
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(); 
  }
}
  Permalink  
Comments
Member 9430088 at 11-Jan-13 5:25am
   
Sorry its still not working
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

You can use DataAdapter to get table from database :
conn.Open();
OleDbDataAdapter ad = new OleDbDataAdapter("", conn);
DataTable dt = new DataTable();
ad.Fill(dt);
gridView.DataSource = dt;
  Permalink  
v4
Comments
Member 9430088 at 11-Jan-13 5:25am
   
sorry its still not working
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Hi.You can refer to the following code.
    
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.
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 483
1 Maciej Los 360
2 OriginalGriff 308
3 Yogesh Kumar Tyagi 275
4 CHill60 220
0 OriginalGriff 6,367
1 Sergey Alexandrovich Kryukov 5,296
2 Maciej Los 3,384
3 Peter Leow 3,259
4 DamithSL 2,490


Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 11 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100