Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Merging two datatables in a single dataset

Posted on 2006-04-06
5
Medium Priority
?
597 Views
Last Modified: 2010-05-18
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
0
Comment
Question by:tmace74
5 Comments
 
LVL 14

Expert Comment

by:PockyMaster
ID: 16398247
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
0
 
LVL 28

Accepted Solution

by:
iboutchkine earned 2000 total points
ID: 16399576
This sample will show you how to merge two datasets into one dataset. When it starts up it will pull data
from two different xml files and populate the two grids then all you have to do is hit button 3 to join the
bottom grid to the top grid. Hit button 1 or 2 to refresh the respective grids under them. This sample shows
using the ignore action so that it only looks for a match and updates that match using the primary ID and
any extra records get ignored. What makes this sample work is making sure you got your primary keys
setup correctly.


Imports System.IO

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "
#End Region
    Private LoadDS1 As New DataSet("Imported DataSet")
    Private LoadDS2 As New DataSet("Imported DataSet")
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            Dim XMLReader As New Xml.XmlTextReader(Application.StartupPath & "\test2.txt")
            LoadDS1.ReadXml(XMLReader)
            XMLReader.Close()
            Dim DT As New DataTable()
            DT = LoadDS1.Tables(0)
            DataGrid1.DataSource = DT
            CurDir(Application.StartupPath)

        Catch err As Exception
            MsgBox(err.Message)
        End Try
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Try
            Dim XMLReader As New Xml.XmlTextReader(Application.StartupPath & "\test1.cla.txt")
            LoadDS2.ReadXml(XMLReader)
            XMLReader.Close()

            Dim DT As New DataTable()
            DT = LoadDS2.Tables(0)
            DataGrid2.DataSource = DT
            CurDir(Application.StartupPath)
        Catch err As Exception
            MsgBox(err.Message)
        End Try
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim Key1() As DataColumn = {LoadDS1.Tables("licenseview").Columns("moduleid")}
        LoadDS1.Tables("licenseview").PrimaryKey = Key1
        LoadDS1.EnforceConstraints = True

        Dim Key2() As DataColumn = {LoadDS2.Tables("licenseview").Columns("moduleid")}
        LoadDS2.Tables("licenseview").PrimaryKey = Key2
        LoadDS2.EnforceConstraints = True

        LoadDS1.Merge(LoadDS2, False, MissingSchemaAction.Ignore)
    End Sub
End Class
0
 

Author Comment

by:tmace74
ID: 16400446
I am using the dataset in a Crystal Reports file to generate a report.  
0
 
LVL 28

Expert Comment

by:iboutchkine
ID: 16400465
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
0
 
LVL 6

Expert Comment

by:Jayadev Nair
ID: 16406423
Hi
Use the Merge() Method.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
Suggested Courses
Course of the Month11 days, 22 hours left to enroll

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question