5 Replies - 819 Views - Last Post: 20 September 2012 - 04:39 AM Rate Topic: -----

#1 sundog1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 11-September 12

Issue with Deleting Row Data from Gridview

Posted 14 September 2012 - 08:04 AM

Hi Guys,

I need a little advice where I could be going wrong.. Below is the following code which works fine as long as I 'Comment out' the "cashDA.Update(cashOrdersDS.Tables["cashOrders"]);"

Current Delete Button Code:

       private void btnOrderDelete_Click(object sender, EventArgs e)
        {
            System.Data.OleDb.OleDbCommandBuilder cb;
            cb = new System.Data.OleDb.OleDbCommandBuilder(cashDA);

            if (dataGridView2.SelectedRows.Count > 0)
            {
                cashOrdersTable.Rows.RemoveAt(dataGridView2.SelectedRows[0].Index);


                //cashDA.Update(cashOrdersDS.Tables["cashOrders"]);

                MessageBox.Show("Record Deleted");
            }
            else
            {
                MessageBox.Show("Please Select a row");
            }
        }


If I enable the "cashDA.Update(cashOrdersDS.Tables["cashOrders"]);" code snippet it errors with the following message:

Value cannot be null.
Parameter name: dataTable



I have uploaded a Screenshot of the Error also.

Any help please??

Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: Issue with Deleting Row Data from Gridview

#2 int0x90  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 3
  • Joined: 14-September 12

Re: Issue with Deleting Row Data from Gridview

Posted 14 September 2012 - 12:05 PM

This basically means you don't have a table with the key "cashOrders" in cashOrderDS.

If the tab;e you want to update is the one you are removing the row from, then give this code a try:

private void btnOrderDelete_Click(object sender, EventArgs e)
 {
     System.Data.OleDb.OleDbCommandBuilder cb;
     cb = new System.Data.OleDb.OleDbCommandBuilder(cashDA);

     if (dataGridView2.SelectedRows.Count > 0)
     {
         cashOrdersTable.Rows.RemoveAt(dataGridView2.SelectedRows[0].Index);


         cashDA.Update(cashOrdersTable);

         MessageBox.Show("Record Deleted");
     }
     else
     {
         MessageBox.Show("Please Select a row");
     }
 }



Hope that helps!
Was This Post Helpful? 2
  • +
  • -

#3 sundog1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 11-September 12

Re: Issue with Deleting Row Data from Gridview

Posted 17 September 2012 - 01:13 AM

Hi, int0x90.

That idea kinda half-worked. It removes it from the Gridview but does not update the underlying database.

Strangely the 'Save' Button does work without a problem and I have provided the code for this below:

Save button code:
      private void btnOrderSave_Click(object sender, EventArgs e)
        {
 
            System.Data.OleDb.OleDbCommandBuilder cb;
            cb = new System.Data.OleDb.OleDbCommandBuilder(cashDA);

 
            cashOrdersTable.TableName = "cashOrders";

            DataRow DRO = cashOrdersDS.Tables["cashOrders"].NewRow();
            

            DRO["cashQTY"] = Int64.Parse(txtOrderQTY.Text);
            DRO["cashDescription"] = txtBoxDescrip.Text.ToString();
            DRO["cashSupplier"] = txtBoxSupplier.Text.ToString();
            DRO["cashDate"] = dateTimePicker1.Value.ToString();
            DRO["cashCost"] = Convert.ToDouble(txtBoxCost.Text);
            DRO["cashSell"] = Convert.ToDouble(txtBoxSell.Text);
            DRO["cashAccountRef_FKID"] = Int64.Parse(textBox1.Text);
            
            
            cashOrdersDS.Tables["cashOrders"].Rows.Add(DRO);

           
           
            
            cashDA.Update(cashOrdersDS.Tables["cashOrders"]);

            MessageBox.Show("New Order Line Added.");



If the system is able to 'Save' lines to the dataset and underlying database then surely its not too much different for the delete function to work as-well?

thanks for your help on this so far.
Was This Post Helpful? 0
  • +
  • -

#4 int0x90  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 3
  • Joined: 14-September 12

Re: Issue with Deleting Row Data from Gridview

Posted 19 September 2012 - 03:11 PM

Hey sundog1,

I'm not 100% sure on this, but I think the reason it isn't working is because you haven't accepted the changes to the table. Can you give this code a try?

private void btnOrderSave_Click(object sender, EventArgs e)
{

      System.Data.OleDb.OleDbCommandBuilder cb;
      cb = new System.Data.OleDb.OleDbCommandBuilder(cashDA);


      cashOrdersTable.TableName = "cashOrders";

      DataRow DRO = cashOrdersDS.Tables["cashOrders"].NewRow();
      

      DRO["cashQTY"] = Int64.Parse(txtOrderQTY.Text);
      DRO["cashDescription"] = txtBoxDescrip.Text.ToString();
      DRO["cashSupplier"] = txtBoxSupplier.Text.ToString();
      DRO["cashDate"] = dateTimePicker1.Value.ToString();
      DRO["cashCost"] = Convert.ToDouble(txtBoxCost.Text);
      DRO["cashSell"] = Convert.ToDouble(txtBoxSell.Text);
      DRO["cashAccountRef_FKID"] = Int64.Parse(textBox1.Text);
      
      
      cashOrdersDS.Tables["cashOrders"].Rows.Add(DRO);
      cashOrdersDS.Tables["cashOrders"].AcceptChanges(); // This is the new line I've added
     
     
      
      cashDA.Update(cashOrdersDS.Tables["cashOrders"]);

      MessageBox.Show("New Order Line Added.");
}


Was This Post Helpful? 0
  • +
  • -

#5 sundog1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 11-September 12

Re: Issue with Deleting Row Data from Gridview

Posted 20 September 2012 - 02:44 AM

Thanks for that,

I will give it a go.
Was This Post Helpful? 0
  • +
  • -

#6 MrShoes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 312
  • View blog
  • Posts: 488
  • Joined: 13-June 12

Re: Issue with Deleting Row Data from Gridview

Posted 20 September 2012 - 04:39 AM

Be careful with UpdateChanges. Take a look at MSDN about the AcceptChanges method - it will change the state from "Added" or "Modified" to "Unchanged" - meaning they will be ignored when you update the database. This is a common pitfall and often causes changes to remain unsaved.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1