none
Delete a Row from a Access Datatable RRS feed

  • Question

  • I'm trying to delete a row from a Access Datatable using the following code.

                DataTable dt = pricingDataSet1.Test;

                dt.Rows[2].Delete();

                dt.AcceptChanges();

                pricingDataSet1.AcceptChanges();

    When  I step thru the code and use the DataTable Visualizer it shows the row marked for deletion then as deleted but when I check the table itself the row is still in the table.


    why767
    Sunday, January 29, 2012 4:29 AM

Answers

  • There are 6 overloads to the TableAdapter.Update() method. One of which requires a DataRow parameter. You probably just want to use the one that requires the DataTable, so your update would look like this:

    this.textTableAdapter.Update(this.pricingDataSet1.Test);
    

    I don't use TableAdapters (I don't like them, I prefer to have more control over my DataAccess and so I use DataAdapters instead) ... so, consequently, I don't have any good posts on my blog about TableAdapters to point you to. Instead, perhaps a TableAdapter overview might be a good read for you. See if this helps you out at all:

    http://msdn.microsoft.com/en-us/library/bz9tthwx.aspx


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    • Marked as answer by why767 Monday, January 30, 2012 1:27 PM
    Monday, January 30, 2012 6:07 AM

All replies

  • You need to send the updated DataTable back to the Access database. And you need to do that before you do a DataSet or DataTable AcceptChanges()

    I don't know how you got the data into the DataTable to begin with (don't know  if you used a TableAdapter or a DataAdapter), but if you use the .Update() method with either of those, it should send the changes back to your database. I believe it also will do the .AcceptChanges() for you after the database gets updated.

    A DataSet/DataTable is simply an "in memory", disconnected repository for the data in your database. There is no direct link between the two. IOW, adding, updating or deleting from a DataTable does nothing at all to the database. You use the Adapters to retrieve data from the database into your DataSet/DataTable and likewise you must use the Adapters to send updates from the DataSet/DataTable back to the database.


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    Sunday, January 29, 2012 5:34 AM
  • I just created the DataSet and TableAdapter visually and it fills in the Form_Load

      

        private void Form1_Load(object sender, EventArgs e)

            {

    this.testTableAdapter.Fill(this.pricingDataSet1.Test);

            }

     

    If I use the Update() method it takes two arguments “DataRow dataRow”

    this.testTableAdapter.Update(DataRow dataRow);

     

    I don’t know where to get the “DataRow dataRow” from.


    why767
    Monday, January 30, 2012 3:24 AM
  • There are 6 overloads to the TableAdapter.Update() method. One of which requires a DataRow parameter. You probably just want to use the one that requires the DataTable, so your update would look like this:

    this.textTableAdapter.Update(this.pricingDataSet1.Test);
    

    I don't use TableAdapters (I don't like them, I prefer to have more control over my DataAccess and so I use DataAdapters instead) ... so, consequently, I don't have any good posts on my blog about TableAdapters to point you to. Instead, perhaps a TableAdapter overview might be a good read for you. See if this helps you out at all:

    http://msdn.microsoft.com/en-us/library/bz9tthwx.aspx


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    • Marked as answer by why767 Monday, January 30, 2012 1:27 PM
    Monday, January 30, 2012 6:07 AM
  • Thanks, I tried that a dozen times and couldn't get it to work but now it does. I must have just not put it in the correct order.


    why767
    Monday, January 30, 2012 1:27 PM
  • Yeah, a lot of times order matters. You probably still had the .AcceptChanges() in your code prior to the .Update(). A lot of people make that mistake, so don't feel bad.  ;0)
    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    Monday, January 30, 2012 3:28 PM