I have tried so hard to solve my problem, here's what I'm trying to do:
- I have an XML file that I load into a dataset("ds") using
ReadXML, several tables are populated in to the dataset, the one I'm concerned about is ("SalesReceiptRet") and I'll refer to it as the source table. - I have another table in MS Access database, I load it into the same
dataset usingOleDBAdapter.Fill, into a datatable named ("dtTarget") and the adapter's name is ("dbAdapter").
I want to go through all the records in the source table, lookup a field called ("TxnID") to locate the record in the target table. If it does not exist, add it, if it does do another validation and overwrite/update it using the source's row.
here's the code:
Private Sub btnTest_Click(sender As Object, e As EventArgs) Handles btnTest.Click
'Initialize command builder
Dim dbCommandBuilder As New OleDb.OleDbCommandBuilder(dbAdapter)
'Set the primary keys
ds.Tables("dtTarget").PrimaryKey = New DataColumn() {ds.Tables("dtTarget").Columns("TxnID")}
'Find rows
Dim dr As DataRow
Dim lookupRow As DataRow
For Each dr In ds.Tables("SalesReceiptRet").Rows
lookupRow = ds.Tables("dtTarget").Rows.Find(dr.Item(0))
'If the a row with a similar TxnID exists, do the following validation
If Not (lookupRow Is Nothing) Then
If lookupRow.Item(8).ToString <> "Regular" Then
'do something here to overwrite/update the matching row
End If
Else
'If the row does not exist, import it
ds.Tables("dtTarget").ImportRow(dr)
End If
Next
'Update Access
dbAdapter.Update(ds, "dtTarget")
dbConnection.Close()
End Sub
Perhaps I need to mention that both tables have exact column names except for Access "dtTarget" has additional ones, which does not seem to be causing problem at least with the import row.
Any thoughts please?