Start New Discussion within our Software Development Community

Hi Guys...

I seem to be having an issue when trying to create a new row in a Dataset. Below is the following Connection Details to the Access DB.

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
            cashCustom.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/Orders.mdb";
            cashCustomerDS = new DataSet();
            cashcTable = new DataTable();


            string sql = "SELECT * FROM cashCustomers";
            cashDA = new System.Data.OleDb.OleDbDataAdapter(sql, cashCustom);

            foreach (DataRow myRow in cashcTable.Rows)
                listView1.Items[listView1.Items.Count - 1].SubItems.Add(myRow[1].ToString());
                listView1.Items[listView1.Items.Count - 1].SubItems.Add(myRow[4].ToString());

                listView1.Items[listView1.Items.Count - 1].SubItems.Add(myRow[2].ToString());
                listView1.Items[listView1.Items.Count - 1].SubItems.Add(myRow[3].ToString());
                listView1.Items[listView1.Items.Count - 1].SubItems.Add(myRow[5].ToString());
                listView1.Items[listView1.Items.Count - 1].SubItems.Add(myRow[6].ToString());


As you can see from above it connects to the database and Selects all from cashCustomers and lists the items out in a list view.

Now... Here is the code for the Save button.

      private void button1_Click(object sender, EventArgs e)
            if (cashCustomerDS != null && cashCustomerDS.Tables.Count > 0)
                **DataRow dRow = cashCustomerDS.Tables["orders.cashCustomers"].NewRow();**

                dRow[1] = txtAccRef.Text;
                dRow[2] = txtName.Text;
                dRow[3] = txtAddr1.Text;
                dRow[4] = txtAddr2.Text;
                dRow[5] = txtTown.Text;
                dRow[6] = txtCounty.Text;
                dRow[7] = txtTown.Text;


                cashDA.Update(cashCustomerDS, "cashCustomers");

                MessageBox.Show("Cash Account Added");


Data should be pulled from the Linked txt boxes placed on a new row in the dataset and then using the dataAdapter the cashCustomers table should be updated.

Once I input new details into said text boxes the syste bugs out on the high-lighted row above stating the following:

NullReferenceException was Unhandled
Object reference not set to an instance of an object

I'm guessing its because the system believes the Dataset is blank?

any helkp would be great. Thanks in advance.


Its not to do with the fact the table your making the new row from is referred to as orders.cashCustomers is it?

Your then adding the row to cashCustomers.

Hi Mikey,

I'm afraid not, that was test to see if it needed the name of the datbase aswell but still not working..

I dont know if the Dataset gets empited once it fills a Data table? Is that where I could be going wrong?

Shouldnt do, might be worth dropping a break point in the save method and having a look at what the dataset actually contains.

Hmm.. your right...

I added an ELSE clause which should show a message box.

When setting a break point on the IF statement it and then "Stepping into" it jumps straight to the "Nothing in here" message box"

If thats the case and the DS is empty, what is the best way/easiest way to re-populate it?

Ahh Hold up... I might be wrong.

If I select the radio button in debug mode which populates the listview with data from the DS and then rung the "stepping into" it tries to add the row but comes up with the error. I'm guessing this means that there is data in the DS.

The reason I think this is becuase if i run the debug and do not click the 'radio button' to populate the listview and then run "stepping into" it jumps direct to my message box stating "there is nothing here".

I hope that makes sense. :S

Kind of makes sense.

Yes you would have to run the listview populate before you could save because this is where all the tables are added.

Until this is done the dataset would appear null.

That the right understanding?

Correct Mikey

However, When all that is done, this is when I get the NULL message. Which Does not really make any sense.

Hi Mikey,

I've uploaded a screenshot for this isue to show you where I think the Null value could be causing a problem.

Attachments DaniWeb1.PNG 72.42 KB

dRow would be null as its not past the instanciation line (the one its exceptioning on).

I believe you will find the dataset table your doing the .NewRow() on to instanciate the dRow will be the null object.

Hi Mikey,

So you think the cashCustomersDS.Tables["cashCustomers"] no longer exists? in the casCustomerDS?

if thats so, how come it read the IF statment and follows through without reading the IF statement and then Jumping to the 'ELSE' statement.

I completely missed the If statement sorry.

I happen to be using that identical line structure in the code im working on currently and declaring the datarow and instanciating with the .NewRow() works fine for me.

In the watch window look at cashCustomerDS.Tables[0] see what it contains.

Hi Mikey,

Ok.. For some reason I had 2

private void listView1_SelectedIndexChanged(object sender, EventArgs e)

I pasted it into the other 1 and it worked fine! I now have the comment box appearing telling me the tag number! Wh00!

Thats another step closer! :)

God knows what I have 2 Private voices exactly the same! :S

Ahhh Mikey... This is the wrong thread..

The other ones fixed.. this is still an issue :(

Hi Mike,

On this Issue I set the Debugger at the 'Save Button' position and stepped through bit by bit. As soon as the program reached the following code:

DataRow dRow = cashCustomerDS.Tables["cashCustomers"].NewRow();

The 'Watch' Box was completly empty. and the next step brought up the error. So I'm guessing for some reason the Dataset at this point has no data?

Thanks again for your help on this.

Hi Mikey,

Even though I have now re-written this thing from the ground up using Gridview instead of list view and made some great advances I'm still having the same problem with the bloody Save button and that infernal Dataset line!

Below is the new code: (I even added the connection the database AGAIN to try and make sure it got filled!)

      private void btnSaveAcc_Click(object sender, EventArgs e)
            //System.Data.OleDb.OleDbCommandBuilder cbp;
            //cbp = new System.Data.OleDb.OleDbCommandBuilder(cashDA);

            //String cashCustomerSQL = "SELECT * FROM cashCustomers";
            //cashDA = new System.Data.OleDb.OleDbDataAdapter(cashCustomerSQL, cashCustom);

            dataGridView1.DataSource = cashCustomersDS;
            dRow = cashCustomersDS.Tables["cashCustomers"].NewRow();
            dRow[0] = txtBoxAccRef.Text;
            dRow[1] = txtBoxName.Text;
            dRow[2] = txtBoxAddr1.Text;
            dRow[3] = txtBoxAddr2.Text;
            dRow[4] = txtBoxTown.Text;
            dRow[5] = txtBoxPostCode.Text;



Screenshot of Error is included. I think I've used the watch window correctly and its showing nowt. Diddly Squat even when I make it watch something.

Attachments Capture9.PNG 40.04 KB

Can you screenshot the watch window at line 10 of the above code block.

In the watch window type cashCustomersDS.Tables.Count (will be a one line entry) and also cashCustomerDS.Tables[0].Rows.Count (again a one line response).

This article has been dead for over six months. Start a new discussion instead.