How to Write from DataGridView to MS Access Table

  • Thread starter Thread starter ryguy72
  • Start date Start date
R

ryguy72

Guest
I'm running the code below (I have the appropriate references, and Using statements, and stuff). The code looks fine to me, and when I step through, I got right to the end with no errors, but the data from the DataGridView actually isn't written to the Access Table. I'm not sure what's wrong here. Can someone see what I can't see?


private void button1_Click(object sender, EventArgs e)

//remove the semicolon, and add brackets below after line
{
//create the connection string
string connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Ryan\\Desktop\\Coding\\Microsoft Access\\Northwind_2012.mdb";

//create the database query
string query = "SELECT * FROM [OrderDetailsTest]";

//create an OleDbDataAdapter to execute the query
OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, connString);

//create a command builder
OleDbCommandBuilder cBuilder = new OleDbCommandBuilder(dAdapter);

//create a DataTable to hold the query results
DataTable dTable = new DataTable();

//fill the DataTable
dAdapter.Fill(dTable);

//the DataGridView
DataGridView dataGridView1 = new DataGridView();

//BindingSource to sync DataTable and DataGridView
BindingSource bSource = new BindingSource();

//set the BindingSource DataSource
bSource.DataSource = dTable;

//set the DataGridView DataSource
dataGridView1.DataSource = bSource;

// An update function to get the changes back into the database.
dAdapter.Update(dTable);
}


Knowledge is the only thing that I can give you, and still retain, and we are both better off for it.

Continue reading...
 
Back
Top