If you are more comfortable dealing with
DataTables
than
List<>
s, you might consider putting your data into a DataTable using this:
public class DBUtilities
{
public static DataTable ReaderToTable(DbDataReader reader)
{
DataTable newTable = new DataTable();
DataColumn col;
DataRow row;
int i;
for (i = 0; i < reader.FieldCount; i++)
{
col = new DataColumn();
col.ColumnName = reader.GetName(i);
col.DataType = reader.GetFieldType(i);
newTable.Columns.Add(col);
}
while (reader.Read())
{
row = newTable.NewRow();
for (i = 0; i < reader.FieldCount; i++)
{
row[i] = reader[i];
}
newTable.Rows.Add(row);
}
return newTable;
}
}
you would use it in your code like this:
public partial class DtposMDIParentSystem : Form
{
DataTable result = null;
public DtposMDIParentSystem()
{
InitializeComponent();
}
private void DtposMDIParentSystem_Load(object sender, EventArgs e)
{
OleDbConnection aConnection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\AP_AE\Desktop\DTPOS_APP\DataBase\DtposDatabase.accdb;");
OleDbCommand aCommand = new OleDbCommand("SELECT * FROM Food", aConnection);
try
{
aConnection.Open();
OleDbDataReader reader = aCommand.ExecuteReader();
result = DBUtilities.ReaderToTable(reader);
reader.Close();
aConnection.Close();
}
catch (InvalidOperationException ex)
{
MessageBox.Show("Invalid Masseage = " + ex.Message);
}
}