Link to home
Start Free TrialLog in
Avatar of tmace74
tmace74

asked on

Merging two datatables in a single dataset

Hello Experts,

   I am new to VB.NET and am trying to merge two datasets into one by looping through the first using a for Each loop and the second table using a For loop  then I use an if, which doesn't currently work, to compare the UserID in each table, in the Address table the UserID is not always unique so I can't use a parent child relation to do this join. I unfortunately cannot do this join in the database so I have to do a merge in .net.  Any help on how best merge these two tables would be greatly appreciated.

                For Each drSuppData In dsData.Tables("Addresses").Rows
                    For i = 0 To dsData.Tables("Users").Rows.Count - 1
                        TmpUserID = CType(dsData.Tables("Users").Rows(i).Item("UserID"), String)
                        If TmpUserID = (drSuppData.Item("UserID") Then
                            dsData.Tables("Users").Rows(i).Item("Address") = drSuppData.Item("Address")
                          End If
                    Next i
                Next

Thanks,
   Tim
Avatar of PockyMaster
PockyMaster
Flag of Netherlands image

What do you want to do with your data later?

You might consider creating a hashtable lookup first, with the userID as a key and the address as value to have a quick lookup for your suppliers
ASKER CERTIFIED SOLUTION
Avatar of iboutchkine
iboutchkine

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tmace74
tmace74

ASKER

I am using the dataset in a Crystal Reports file to generate a report.  
It does not matter how you created your dataset. The examplpe shows how to merge 2 datasets and enter key into them to eliminate duplicates
Hi
Use the Merge() Method.