Click here to Skip to main content
12,449,919 members (40,620 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# 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
Updated 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 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 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
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160826.1 | Last Updated 11 Jan 2013
Copyright © CodeProject, 1999-2016
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