|
-
Jan 2nd, 2020, 07:19 PM
#1
Thread Starter
Fanatic Member
Update a single row in database
I have 1 parent table and 2 child tables. I've added 4 new rows to the parent DataTable and a with each new parent row I've added a couple of new rows to each child DataTable. I would like to loop through the set of added rows in the parent table to update the changes (row additions) in the underlying 3 tables of the MS Access dB.
Here is my code:
Code:
CType(dsMain.Relations("Task_ActionItems").ChildKeyConstraint, ForeignKeyConstraint).AcceptRejectRule = AcceptRejectRule.Cascade
CType(dsMain.Relations("Task_TaskNotes").ChildKeyConstraint, ForeignKeyConstraint).AcceptRejectRule = AcceptRejectRule.Cascade
For Each rowTask As DataRow In dsMain.Tables("Tasks").Select(vbNullString, vbNullString, DataViewRowState.Added)
newTask(0) = rowTask
Debug.WriteLine("Tasks = " & CStr(dsMain.Tables("Tasks").Select("", "", DataViewRowState.Added).Count))
iTasks += daTasks.Update(newTask) '...persists the DataTable change to the MS Access dB table
Debug.WriteLine("Tasks = " & CStr(dsMain.Tables("Tasks").Select("", "", DataViewRowState.Added).Count))
actionRows = dsMain.Tables("ActionItems").Select("TaskID = " & CLng(rowTask("RecID")))
If actionRows.Count > 0 Then
iActionItems += daActionItems.Update(actionRows) '...persists the DataTable change to the MS Access dB table
End If
noteRows = dsMain.Tables("TaskNotes").Select("TaskID = " & CLng(rowTask("RecID")))
If noteRows.Count > 0 Then
iTaskNotes += daTaskNotes.Update(noteRows) '...persists the DataTable change to the MS Access dB table
End If
Next rowTask
CType(dsMain.Relations("Task_ActionItems").ChildKeyConstraint, ForeignKeyConstraint).AcceptRejectRule = AcceptRejectRule.Cascade
CType(dsMain.Relations("Task_TaskNotes").ChildKeyConstraint, ForeignKeyConstraint).AcceptRejectRule = AcceptRejectRule.Cascade
Since there are 4 new rows in the parent table, the code makes 4 trips through the For Each...Next loop. The Debug.Writeline statements show the count of parent table rows having their DataViewRowState property = "Added" immediately before/after calling the DataAdapter.Update method for the parent table. Since I'm calling the parent table's DataAdapter.Update method and passing it an array of DataRows (newTask) that only has a single DataRow (newTask(0)), I would expect that the first and second Debug.Writeline statements would show the count of parent table rows having their DataViewRowState.Added to decrease by 1 with each trip through the For Each...Next loop.
What is actually happening is that on the first trip through the loop the first Debug.Writeline statement shows "Tasks = 4", the second Debug.Writeline statement shows "Tasks = 0" in the Immediate Window, and the value of iTasks = 1. On the second trip through the loop the first Debug.Writeline statement shows "Tasks = 0", the second Debug.Writeline statement shows "Tasks = 0" in the Immediate Window, and the value of iTasks = 0. It appears that all 4 new parent table rows are updated in the Accesss dB on the first pass through the loop.
What can I do/change to get the parent table's DataAdapter.Update method to only update 1 row per trip through the loop?
Last edited by Mark@SF; Jan 2nd, 2020 at 07:59 PM.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|