0

First of all, here is my code:

private void Form5_Load(object sender, EventArgs e)
{
    string strProvider = @"Provider = Microsoft.ACE.OLEDB.12.0; Data Source = c:\Users\name\Documents\myprogramms\example.accdb";
    string strSql = "Select * from score";
    OleDbConnection con = new OleDbConnection(strProvider);
    OleDbCommand cmd = new OleDbCommand(strSql, con);
    con.Open();
    cmd.CommandType = CommandType.Text;
    OleDbDataAdapter da = new OleDbDataAdapter(cmd);
    DataTable scores = new DataTable();
    da.Fill(scores);
    data_example.DataSource = scores;
}

My goal is to display the data out of a MS Access Database. My codes has no error messages, but everytime I try to open the Data Grid View its completely empty.

3
  • What does every time I try to open the DataGridView mean? Of course, you don't open a DataGridView. Is this DGV child of Form5 or some other Form? Commented May 7, 2020 at 0:04
  • @Jimi yeah sorry, thats what I meant. Poor choice of words by my side... Commented May 7, 2020 at 5:43
  • ...And what's that's what I meant referring to? Do you mean that the data_example is a DGV that belongs to another Form? If that's so, how did it get in Form5? You question lacks in details. Commented May 7, 2020 at 5:48

1 Answer 1

1

your code snippet looks good. I cannot see any obvious issue.

Try to put breakpoint at the end of your method and check if datatable contains rows and rows contains any values in ItemArray.

Make sure your data_example DataGridView control is set AutoGenerateColumns = true (default) and check you have no other data source defined for your data_example DataGridView control. This code works for me.

private void Form1_Load(object sender, EventArgs e)
{
    data_example.AutoGenerateColumns = true;

    string strProvider = @"Provider = Microsoft.ACE.OLEDB.12.0; Data Source = c:\aaa\CinemaBooking.accdb";
    string strSql = "Select * from Booking";

    using (OleDbConnection con = new OleDbConnection(strProvider))
    {
        using (OleDbCommand cmd = new OleDbCommand(strSql, con))
        {
            using (OleDbDataAdapter da = new OleDbDataAdapter(cmd))
            {
                con.Open();
                var scores = new DataTable();
                da.Fill(scores);
                data_example.DataSource = scores.DefaultView;
                con.Close();
            }
        }
    }
}

Maybe you can try to fill dataset with adapter and then use named table as datasource:

var dtSet = new DataSet();  
da.Fill(dtSet, "Booking");  
data_example.DataSource = dtSet.Tables["Booking"].DefaultView;  
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your time and your answer! I found another way to realize my plans. When you add a DataGridView to your windows Form, vs2017 asks you if you want to connect it with a DB and you can choose it right away. I haven't considered that way because I didn't read anything about it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.