Geo
Geo

Reputation: 363

Adding Access database to C# dataGridView (using only code)

I have 2 dataGridViews on a form, and when I run the app, nothing shows up. I believe that adding the dataGridView information in the code, like below, I don't need to add the database using Visual Studio's "Server Explorer". That would be redundant and/or change the fill outcome of the dataGridView, right? Am I missing something in my code??

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace WindowsFormsApplication1
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
      DataSet ds = new DataSet();
      OleDbDataAdapter daOrders = new OleDbDataAdapter();
      OleDbDataAdapter daReceived = new OleDbDataAdapter();
      OleDbConnection vcon = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;data source=C:\Query Form\Database.accdb");    

      OleDbCommand slctOrders = new OleDbCommand("SELECT * FROM script_Orders ORDER BY script");
      daOrders.SelectCommand = slctOrders;
      daOrders.Fill(ds, "tblOrders");

      OleDbCommand slctReceived = new OleDbCommand("SELECT * FROM script_Received");
      daOrders.SelectCommand = slctReceived;
      daOrders.Fill(ds, "tblReceived");

      dgOrders.DataSource = ds.Tables ["tblOrders"];
      dgReceived.DataSource = ds.Tables ["tblReceived"];
    }
  }
}

Upvotes: 1

Views: 8799

Answers (1)

Kevin DiTraglia
Kevin DiTraglia

Reputation: 26058

Try passing the connection into the data adapter...

OleDbCommand slctOrders = new OleDbCommand("SELECT * FROM script_Orders ORDER BY script", vcon);

I'm not sure if that is all you are missing (you might also need to open the connection, vcon.Open()), but there has to be some some link between the connection object and the adapters.

Upvotes: 2

Related Questions