Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and the Yahoo Answers website is now in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

C# Populate DataTable from Access?

I am having some trouble binding a datatable returned from a query to a datagridview. I believe the problem is in the GetTableFromQuery(...) method. Debug shows no table rows being populated after the query execution. The table contains no data in the datagridview after binding the datasource.

Here's the query and call to database operation.

private void buttonFilterCustomers_Click(object sender, EventArgs e)

{

DataTable dt = new DataTable();

string query = "SELECT * FROM Customers WHERE '" + comboBoxFilterCustomers.SelectedItem.ToString() + "' = '" + textBoxFilterValueCustomers.Text.ToString() + "'";

dt = DatabaseOperations.GetTableFromQuery(query, null, CommandType.Text);

dataGridViewCustomers.DataSource = dt;

}

Here's the database operation. Sorry for the lack of indentation.

public static DataTable GetTableFromQuery(string query, Dictionary<string, object> parameters, CommandType commandType)

{

DataTable dataTable = new DataTable();

using (OleDbConnection conn = GetConnection())

{

using (OleDbCommand cmd = new OleDbCommand(query, conn))

{

cmd.CommandType = commandType;

if (parameters != null)

{

foreach (KeyValuePair<string, object> parameter in parameters)

{

cmd.Parameters.AddWithValue(parameter.Key, parameter.Value);

}

}

using (OleDbDataAdapter adapter = new OleDbDataAdapter(cmd))

{

adapter.Fill(dataTable);

}

}

}

return dataTable;

}

1 Answer

Relevance
  • Anonymous
    7 years ago
    Favorite Answer

    does your select statement use "like" ?

    if it does make sure you change

    like 'A*'

    to

    like 'A%'

    the first works in Access SQL but does not work in C#

    try making your sql simple

    just do a simple

    select * from Customers

    to see if you're getting anything back at all

    are you sure you're actually connected ?

    maybe that's failing ?

    and you can paste your code in pastebin.com

    that will preserve the formatting and indentation

Still have questions? Get your answers by asking now.