Results 1 to 4 of 4

Thread: DataAdapter.Update method

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2013
    Location
    San Francisco, CA
    Posts
    528

    DataAdapter.Update method

    As per Microsoft's documentation, there are several ways to call the DataAdapter.Update method by passing it one of the following: an array of DataRow objects, a DataSet object, or a DataTable object.

    And furthermore, the AcceptChanges method for the DataRow, DataSet, or DataTable should also be implicitly called by the DataAdapter.Update method.

    I have a MS Access database as my datasource. I use a DataAdapter and I've configured its DeleteCommand, InsertCommand, and UpdateCommand properties.

    If I call the DataAdapter.Update method and pass it a DataTable, the AcceptChanges method of the DataTable is not implicitly called by the DataAdapter.Update method.

    Code:
    dtTemp = dsMain.Tables("Categories").GetChanges(DataRowState.Modified)
    daCategories.Update(dtTemp)
    If instead I call the DataAdapter.Update method and pass it an array of DataRow objects, then the AcceptChanges method for each changed DataRow is implicitly called by the DataAdapter.Update method.

    Code:
    rows = dsMain.Tables("Categories").Select(vbNullString, vbNullString, DataViewRowState.ModifiedCurrent)
    daCategories.Update(rows)
    I can see this behavior by checking the if the DataTable has changes before and after calling the DataAdapter.Update method.

    Code:
    Debug.Print(dsMain.Tables("Categories").GetChanges Is Nothing)
    Since the Microsoft documentation claims otherwise, is there something that I'm missing?
    Last edited by Mark@SF; Sep 14th, 2019 at 02:58 PM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: DataAdapter.Update method

    Why are you calling GetChanges in the first place? This:
    vb.net Code:
    1. dtTemp = dsMain.Tables("Categories").GetChanges(DataRowState.Modified)
    suggests that you want to isolate those rows with a RowState of Modified, i.e. existing rows that have been edited, from those with a RowState of Added, i.e. new rows, or Deleted, i.e. existing rows that have been deleted. Do you want to do that? If so, this line doesn't really make sense:
    vb.net Code:
    1. Debug.Print(dsMain.Tables("Categories").GetChanges Is Nothing)
    That is checking whether the DataTable contains changes of ANY kind and not just edits. If you do care about changes of any kind, why are you calling GetChanges before Update in the first place?

    That's by the by though. The real issue here is that GetChanges returns a completely new DataTable, populated with completely new DataRows. When you do this:
    vb.net Code:
    1. dtTemp = dsMain.Tables("Categories").GetChanges(DataRowState.Modified)
    2. daCategories.Update(dtTemp)
    The AcceptChanges method of dtTemp, and thus the AcceptChanges method of every DataRow in dtTemp, does get called. If you were to actually examine dtTemp afterwards then you'd see that. The thing is, that has exactly zero effect on the original DataTable, i.e. dsMain.Tables("Categories"), which is the whole point. Calling GetChanges allows you to mess around with specific changes from a DataTable without affecting the whole DataTable. The idea is that you do whatever you need to do on that DataTable and any others in whatever order you want, then you call AcceptChanges explicitly when you're done. For example, let's say that you have a parent DataTable and a child DataTable. You need to delete child records before deleting parent records but you need to insert parent records before inserting child records, so you can 't just do all of one DataTable and then all of the other. You would handle that situation something like this:
    vb.net Code:
    1. Dim childDeleteTable = childTable.GetChanges(DataRowState.Deleted)
    2. Dim parentDeleteTable = parentTable.GetChanges(DataRowState.Deleted)
    3.  
    4. childAdapter.Update(childDeleteTable)
    5. parentAdapter.Update(parentDeleteTable)
    6.  
    7. Dim parentUpdateTable = parentTable.GetChanges(DataRowState.Modified)
    8. Dim childUpdateTable = childTable.GetChanges(DataRowState.Modified)
    9.  
    10. parentAdapter.Update(parentUpdateTable)
    11. childAdapter.Update(childUpdateTable )
    12.  
    13. Dim parentInsertTable = parentTable.GetChanges(DataRowState.Added)
    14. Dim childInsertTable = childTable.GetChanges(DataRowState.Added)
    15.  
    16. parentAdapter.Update(parentInsertTable)
    17. childAdapter.Update(childInsertTable )
    18.  
    19. parentTable.AcceptChanges()
    20. childTable.AcceptChanges()
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2013
    Location
    San Francisco, CA
    Posts
    528

    Re: DataAdapter.Update method

    jmc -

    You rock! My learning point for today...

    The real issue here is that GetChanges returns a completely new DataTable, populated with completely new DataRows.
    Makes sense, now that you've explained it. BTW, where in the Microsoft documentation is that nugget of knowledge?

    Thanks for your help.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: DataAdapter.Update method

    The documentation says that GetChanges gets a copy of the original DataTable and the authors probably just assumed that you would know that a DataRow is always associated with a specific DataTable. I'm fairly certain that you can't even remove a DataRow from a DataTable and then add it to another, let alone add it to another without removing it first. Given that a DataRow has a Table property that specifies the DataTable it belongs to, that's indicative of the fact that it can only belong to one DataTable.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width