How do I make a copy of a data table, make changes to it and then update Access database so that the

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I would like to make a copy of the contents of a datagrid, but first updating one of the field values and then write the updated copy back to an Access database. I also want to preserve the original data records in the datagrid in the database. Essentially
I want to make a copy of the datagrid with some changes, but preserve the original records in the database. Here is my code
Dim cnPurchasedEquipment As New OleDbConnection(strinfo)<br/>
Dim da_TempPurchasedEquipment As New OleDbDataAdapter<br/>
da_TempPurchasedEquipment.SelectCommand = New OleDbCommand("SELECT * FROM PurchasedEquipment WHERE EstimateNumber = " & EstimateNumber.Text & "", cnPurchasedEquipment)<br/>
Dim cb_PurchasedEquipment As OleDbCommandBuilder = New OleDbCommandBuilder(da_TempPurchasedEquipment)<br/>
Dim ds_PurchasedEquipmentSchema As New DataSet
Dim equipmentTable As DataTable<br/>
If bReuseFlag Then<br/>
equipmentTable = ds_PurchasedEquipment.Tables("PurchasedEquipment").Copy()<br/>
Else<br/>
equipmentTable = ds_PurchasedEquipment.Tables("PurchasedEquipment")<br/>
End If
For Each equipmentRow In equipmentTable.Rows<br/>
equipmentRow("EstimateNumber") = EstimateNumber.Text

More updates ....<br/>
Next
So now equipmentTable will have a new estimate number if bReuseFlag is true, otherwise it will have the original estimate number
cnPurchasedEquipment .Open()<br/>
If ds_PurchasedEquipment.HasChanges Then<br/>
ds_PurchasedEquipmentSchema= ds_PurchasedEquipment.GetChanges()
da_TempPurchasedEquipment.Update(ds_PurchasedEquipmentSchema, "PurchasedEquipment")
<br/>
End If<br/>
cnPurchasedEquipment .Close()
The above only will only update the original purchased equipment table, but not the copied one in which I changed the estimate number. So I bReuseFlag is True, the table is not updated. But if false, I can update the original table.


View the full article
 
Back
Top