0

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?

Edited by mike_2000_17: Fixed formatting

4
Contributors
5
Replies
14
Views
7 Years
Discussion Span
Last Post by segal87
0

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?

0

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????

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.