I'm pretty nee to all this stuff, I have no problems reading from the database but writing new records and updating fields etc is proving to be quite difficult. The following code is just meant to create a new record in a table, I started the field count at [2] as [1] is an autonumber that I presume would be generated upon creation of the record.
private string connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};
DataSet ds1 = new DataSet("Customer");
System.Data.OleDb.OleDbDataAdapter da;
{
connString = String.Format(connString, @"C:\Documents and Settings\Nick\Desktop\PPSP V2.0\KCH Car Rental V2.0\PPSP Car Rentals.accdb");
OleDbConnection cn = new OleDbConnection(connString);
cn.Open();
string sql = "SELECT * FROM Customer";
da = new System.Data.OleDb.OleDbDataAdapter(sql, cn);
da.Fill(ds1);
DataRow dRow = ds1.Tables["Customer"].NewRow();
dRow[2] = txtFName.Text;
dRow[3] = txtSName.Text;
dRow[4] = txtDOB.Text;
dRow[5] = txtAdd1.Text;
dRow[6] = txtAdd2.Text;
dRow[7] = txtCounty.Text;
dRow[8] = txtPostCode.Text;
ds1.Tables["Customer"].Rows.Add(dRow);
da.Update(ds1, "Customers");
cn.Close();
MessageBox.Show("Customer Added");
}
An exception is thrown at run time on the line DataRow dRow = ds1.Tables["Customer"].NewRow();
Any ideas what I'm doing wrong or if theres a better way of doing what I want?