Shan
Shan

Reputation: 439

VB.Net DataTable nested relationship how?

enter image description here

enter image description here

Updated: I am trying to create a XML with the schema shown in the screenshot(link). Anybody tell me how to add the relation to achieve this or modify the Datatable to achieve this?

        Data_Set.Relations.Add("ProdCategory", tbl_Product.Columns("pID"), tbl_Category.Columns("cID"))
    Data_Set.Relations("ProdCategory").Nested = True

    Data_Set.Relations.Add("CatogoryItems", tbl_Category.Columns("cID"), tbl_Items.Columns("iID"))
    Data_Set.Relations("CatogoryItems").Nested = True

    'Data_Set.Relations.Add("ProdItems", tbl_Product.Columns("pID"), tbl_Iems.Columns("iID"))
    'Data_Set.Relations("ProdItems").Nested = True

I am stuck with the above code.

and also i should able to read xml back to dataset with read xml.

Upvotes: 3

Views: 2162

Answers (1)

Mr. Tripodi
Mr. Tripodi

Reputation: 827

Your code looks as if you are attempting to use different Parent/Child columns, Such as you are linking the productid in parent table to categoryid in child table, etc... That is going to give undesired result. My closest guess to what you want:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim DtSet As New DataSet
    With DtSet
        .Tables.Add("Product")
        With .Tables("Product")
            .Columns.Add("ProductID", GetType(String))
            .Rows.Add("P1")
            .Rows.Add("P2")
        End With

        .Tables.Add("Category")
        With .Tables("Category")
            .Columns.Add("ProductID", GetType(String))
            .Columns.Add("CategoryID", GetType(String))
            .Rows.Add("P1", "C1")
            .Rows.Add("P1", "C2")
            .Rows.Add("P2", "C3")
            .Rows.Add("P2", "C4")
        End With

        .Tables.Add("Items")
        With .Tables("Items")
            .Columns.Add("ProductID", GetType(String))
            .Columns.Add("CategoryID", GetType(String))
            .Columns.Add("Items", GetType(String))
            .Rows.Add("P1", "", "Item1")
            .Rows.Add("P2", "", "Item2")
            .Rows.Add("", "C2", "Item3")
            .Rows.Add("", "C3", "Item4")
            .Rows.Add("", "C4", "Item5")
        End With

        .Relations.Add(New DataRelation("ProductToItems", .Tables("Product").Columns("ProductID"),
                                                                  .Tables("Items").Columns("ProductID"), False))

        .Relations.Add(New DataRelation("ProductToCategory", .Tables("Product").Columns("ProductID"),
                                                                     .Tables("Category").Columns("ProductID"), False))

        .Relations.Add(New DataRelation("CategoryToItems", .Tables("Category").Columns("CategoryID"),
                                                                   .Tables("Items").Columns("CategoryID"), False))
        For Each Relation As DataRelation In .Relations
            Relation.Nested = True
        Next
    End With

    DtSet.WriteXml("C:\Data\Data.XML", XmlWriteMode.IgnoreSchema)

End Sub

Because you have null values in your example you cannot enable constraints.

Upvotes: 5

Related Questions