making a connection with an Access database

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hey, im trying to learn the system.data (ado.net) classes of C#.
Now i wont to experiment a little with a data adapter: but when I try to make a connection with a microsoft Access file i get an system.InvalidOperationException. The only thing I want to do is make a connection with this access file and then put the data
in a datagridview, if i change something in the datagridview i want it also changed in the access file. But I cant experiment (and learn) because something is wrong with the connection:
this is my (very simple, straight forward not yet object oriented trial code:)

<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; using System;
<span style="color:Blue; using System.Collections.Generic;
<span style="color:Blue; using System.ComponentModel;
<span style="color:Blue; using System.Data;
<span style="color:Blue; using System.Drawing;
<span style="color:Blue; using System.Linq;
<span style="color:Blue; using System.Text;
<span style="color:Blue; using System.Windows.Forms;
<span style="color:Blue; using System.Data.OleDb;


<span style="color:Blue; namespace WfResidentenNoordhinder
{
<span style="color:Blue; public <span style="color:Blue; partial <span style="color:Blue; class Form1 : Form
{

<span style="color:Green; //Here I get my error:
OleDbConnection con = <span style="color:Blue; new OleDbConnection(<span style="color:#A31515; @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:UsersRobertDesktopresidentenlijst.mdb; User ID=admin; Password=;");

<span style="color:Blue; public Form1()
{
InitializeComponent();
}

<span style="color:Blue; private <span style="color:Blue; void btnInladenGegevens_Click(<span style="color:Blue; object sender, EventArgs e)
{
OleDbCommand cmdSelect = <span style="color:Blue; new OleDbCommand();
OleDbDataAdapter da = <span style="color:Blue; new OleDbDataAdapter(cmdSelect);
DataSet ds1 = <span style="color:Blue; new DataSet();
cmdSelect.Connection = con;
cmdSelect.CommandText = <span style="color:#A31515; "SELECT * FROM [residentenlijst facturatie]";
da.Fill(ds1, <span style="color:#A31515; "Residenten");
BindingSource bsource = <span style="color:Blue; new BindingSource();
bsource.DataSource = ds1;
dgvGegevensResidenten.DataSource = bsource;

<span style="color:Blue; try
{
ds1.Tables.Add(<span style="color:#A31515; "Residenten");

}
<span style="color:Blue; catch (Exception ex)
{
MessageBox.Show(<span style="color:#A31515; "Something is going horribly wrong!");
}





<span style="color:Blue; finally
{
cmdSelect.Dispose();
da.Dispose();
<span style="color:Green; //con.Close();
con.Dispose();
}
}

<span style="color:Blue; private <span style="color:Blue; void dgvGegevensResidenten_CellContentClick(<span style="color:Blue; object sender, DataGridViewCellEventArgs e)
{

}
}
}
[/code]
<br/>
<br/>


View the full article
 
Back
Top