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?

What's the exception?

The exception for the highlighted line is;

NullReferenceException was unhandled

I'm not sure what part of the code this is reffering to, are there any errors or reasons you can see that would stop the code from working correctly?

Debug your code to know which line raises this exception.
- Put breakpoint at ConnString and begin debugging.

After the row "da.Fill(ds1);"

I changed the code to this:

DataTable dt = ds1.Tables[0];
DataRow dRow = dt.NewRow();

and then it's not null Object.
but I stuck in the update command -

"da.Update(ds1, "Customers");"

and I don't know how to update.

any suggestions????

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.