I am trying to execute an SQL command on an Access Database over an OleDbConnection using C# and use that information to fill a DataGridView on a windows form. I have opened the connection, stated the query, and executed it, but I cannot find how to output the results to the DataGridView on the windows form (named dataOutput).
private void Query()
{
string cmdText = "SELECT * FROM RetentionTable " +
"WHERE [DateTime] BETWEEN '" + getDateTimeFrom("") + "' AND '" + getDateTimeTo("") + "'";
string ConnectionPath = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=RetentionDB.mdb";
try
{
OleDbConnection cn = new OleDbConnection(ConnectionPath);
DataSet objDataSet = new DataSet();
OleDbDataAdapter objDataAdapter = new OleDbDataAdapter();
if (cn.State.Equals(ConnectionState.Closed))
{
cn.Open();
}
OleDbCommand OleDbSearch = new OleDbCommand(cmdText, cn);
OleDbSearch.ExecuteNonQuery();
objDataAdapter.Fill(objDataSet);
dataOutput.DataSource = objDataSet;
cn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
MessageBox.Show(ex.StackTrace.ToString());
}
}
From what I can see, the query is being executed correctly, but the issue comes when trying to use the objDataAdapter.Fill. I guess I am not understanding how to fill the DataSet with the output from the query. Any help would be much appreciated. Thanks!