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);


                string sql = "SELECT * FROM Customer";
                da = new System.Data.OleDb.OleDbDataAdapter(sql, cn);

                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;


                da.Update(ds1, "Customers");


                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?

Recommended Answers

What's the exception?

Jump to Post

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

Jump to Post

All 5 Replies

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 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.