I created a C# application and integrated an Access DB (with 3 populated tables).
Now I try to get data from the Access DB-tables to display it in a TextBox, but nothing is visible.
Thank you in advance.
UPDATE: namespace –> using System.Data.OleDb;
Here is the code:
OleDbDataReader myReader;
OleDbConnection myCon;
OleDbCommand myQuery;
public Form1()
{
InitializeComponent();
myCon = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\UserName\\Desktop\\MyClubAdministration\\MyClubAdministrationDB.accdb;Persist Security Info=False;");
myCon.Open();
myQuery = new OleDbCommand("select * from Lid, Lidgeld, Lidmaatschap", myCon);
myReader = myQuery.ExecuteReader();
}
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Text = myReader.GetInt32(0).ToString();
}
You need to Read before GetInt32 on an OleDBDataReader
However this code is very error prone.
Try to isolate the database operarations in a private methods and call just from one point without splitting between constructor and form_load.
call that method after InitializeComponent();
As a side note, I will avoid the globals for the database operations. With connection pooling the performance cost to open and reopen a connection is really minimal, while the complexity of housecleaning grows exponentially.