none
Why does DataRow.GetParentRow() always return Nothing? RRS feed

  • Question

  • I have a typed dataset with 2 tables: Parent & Child.  In the designer I add a relation between the two tables.  When I fill the two tables then call ChildDataRow.GetParentRow(relationName) it always returns Nothing despite there being a parent row, why?

    Ryan

    Wednesday, December 12, 2018 1:50 AM

Answers

  • Hi,

    Please provide your code so we can assist you,my code works well.

    Imports System.Data.SqlClient
    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim ds As DataSet = New DataSet("ds")
            Dim tbClass As DataTable = New DataTable("tbClass")
            Dim tbBoard As DataTable = New DataTable("tbBoard")
            ds.Tables.Add(tbClass)
            ds.Tables.Add(tbBoard)
            Dim ClassID As DataColumn = New DataColumn("ClassID", GetType(System.String))
            Dim ClassName As DataColumn = New DataColumn("ClassName", GetType(System.String))
            ClassID.AllowDBNull = False
            tbClass.Columns.Add(ClassID)
            tbClass.Columns.Add(ClassName)
            tbClass.PrimaryKey = New DataColumn() {ClassID}
            Dim BoardID As DataColumn = New DataColumn("BoardID", GetType(System.String))
            Dim BoardName As DataColumn = New DataColumn("BoardName", GetType(System.String))
            Dim BoardClassID As DataColumn = New DataColumn("BoardClassID", GetType(System.String))
            BoardID.AllowDBNull = False
            tbBoard.Columns.Add(BoardID)
            tbBoard.Columns.Add(BoardName)
            tbBoard.Columns.Add(BoardClassID)
            tbBoard.PrimaryKey = New DataColumn() {BoardID}
    
            For i As Integer = 1 To 5
                Dim tbClassRow As DataRow = tbClass.NewRow()
                tbClassRow("ClassID") = Guid.NewGuid()
                tbClassRow("ClassName") = String.Format("Classification{0}", i)
                tbClass.Rows.Add(tbClassRow)
                Dim tbBoardRow As DataRow = tbBoard.NewRow()
                tbBoardRow("BoardID") = Guid.NewGuid()
                tbBoardRow("BoardName") = String.Format("Section{0}", i)
                tbBoardRow("BoardclassID") = tbClassRow("ClassID")
                tbBoard.Rows.Add(tbBoardRow)
            Next
    
            ds.Relations.Add("RelationBetweenClassAndBoard", ClassID, BoardClassID)
            Dim dtParent As DataTable = ds.Relations("RelationBetweenClassAndBoard").ParentTable
            Dim dtChild As DataTable = ds.Relations("RelationBetweenClassAndBoard").ChildTable
    
            If dtChild Is Nothing Then
                Return
            End If
            Dim parentRow As DataRow
    
            For Each row As DataRow In dtChild.Rows
                parentRow = row.GetParentRow("RelationBetweenClassAndBoard")
                MsgBox( "child row: " & row(1))
                MsgBox( "parent row: " & parentRow(1))
            Next
        End Sub
    
    End Class
    

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Ryan0827 Friday, March 1, 2019 6:03 PM
    Wednesday, December 12, 2018 2:48 AM
  • Hello,

    With a typed DataSet (meaning you created it via the Visual Studio data wizard) you should not need to create a relationship in the xsd file but instead the relationship really should always be setup inside the database then when using the wizard if the relationship is proper in the xsd you will see this.

    From here when you drop the master table (in the above Customers) onto the form then add the child (in this case Orders) from under the Customers, not directly from the Orders tree node you automatically get a master detail view from the two DataGridView controls.

    If when you go to get the master (in this case Customers) from the Orders via the Orders BindingSource as shown below where there is a customer else there would be no order row.

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.OrdersTableAdapter.Fill(Me.NorthWindAzureDataSet.Orders)
            Me.CustomersTableAdapter.Fill(Me.NorthWindAzureDataSet.Customers)
        End Sub
    
        Private Sub CustomersBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) _
            Handles CustomersBindingNavigatorSaveItem.Click
    
            Me.Validate()
            Me.CustomersBindingSource.EndEdit()
            Me.TableAdapterManager.UpdateAll(Me.NorthWindAzureDataSet)
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            MessageBox.Show(
                CType(OrdersBindingSource.Current, DataRowView).
                               Row.GetParentRow("FK_Orders_Customers2").
                               Field(Of Integer)("CustomerIdentifier").ToString())
    
    
        End Sub
    End Class

    I can valid GetParentRow is working via the MessageBox e.g.

    Note I left the CustomerIdentifier column visible so you can see this works.

    Since this is not working for you

    • You didn't drag-drop the DataGridView controls on the form correctly
    • The relationship is bad (can be many reasons but this goes back to creating the relationship in the database and not in Visual Studio).

    Hope this helps.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Wednesday, December 12, 2018 3:38 AM
    Moderator

All replies

  • Hi,

    Please provide your code so we can assist you,my code works well.

    Imports System.Data.SqlClient
    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim ds As DataSet = New DataSet("ds")
            Dim tbClass As DataTable = New DataTable("tbClass")
            Dim tbBoard As DataTable = New DataTable("tbBoard")
            ds.Tables.Add(tbClass)
            ds.Tables.Add(tbBoard)
            Dim ClassID As DataColumn = New DataColumn("ClassID", GetType(System.String))
            Dim ClassName As DataColumn = New DataColumn("ClassName", GetType(System.String))
            ClassID.AllowDBNull = False
            tbClass.Columns.Add(ClassID)
            tbClass.Columns.Add(ClassName)
            tbClass.PrimaryKey = New DataColumn() {ClassID}
            Dim BoardID As DataColumn = New DataColumn("BoardID", GetType(System.String))
            Dim BoardName As DataColumn = New DataColumn("BoardName", GetType(System.String))
            Dim BoardClassID As DataColumn = New DataColumn("BoardClassID", GetType(System.String))
            BoardID.AllowDBNull = False
            tbBoard.Columns.Add(BoardID)
            tbBoard.Columns.Add(BoardName)
            tbBoard.Columns.Add(BoardClassID)
            tbBoard.PrimaryKey = New DataColumn() {BoardID}
    
            For i As Integer = 1 To 5
                Dim tbClassRow As DataRow = tbClass.NewRow()
                tbClassRow("ClassID") = Guid.NewGuid()
                tbClassRow("ClassName") = String.Format("Classification{0}", i)
                tbClass.Rows.Add(tbClassRow)
                Dim tbBoardRow As DataRow = tbBoard.NewRow()
                tbBoardRow("BoardID") = Guid.NewGuid()
                tbBoardRow("BoardName") = String.Format("Section{0}", i)
                tbBoardRow("BoardclassID") = tbClassRow("ClassID")
                tbBoard.Rows.Add(tbBoardRow)
            Next
    
            ds.Relations.Add("RelationBetweenClassAndBoard", ClassID, BoardClassID)
            Dim dtParent As DataTable = ds.Relations("RelationBetweenClassAndBoard").ParentTable
            Dim dtChild As DataTable = ds.Relations("RelationBetweenClassAndBoard").ChildTable
    
            If dtChild Is Nothing Then
                Return
            End If
            Dim parentRow As DataRow
    
            For Each row As DataRow In dtChild.Rows
                parentRow = row.GetParentRow("RelationBetweenClassAndBoard")
                MsgBox( "child row: " & row(1))
                MsgBox( "parent row: " & parentRow(1))
            Next
        End Sub
    
    End Class
    

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Ryan0827 Friday, March 1, 2019 6:03 PM
    Wednesday, December 12, 2018 2:48 AM
  • Hello,

    With a typed DataSet (meaning you created it via the Visual Studio data wizard) you should not need to create a relationship in the xsd file but instead the relationship really should always be setup inside the database then when using the wizard if the relationship is proper in the xsd you will see this.

    From here when you drop the master table (in the above Customers) onto the form then add the child (in this case Orders) from under the Customers, not directly from the Orders tree node you automatically get a master detail view from the two DataGridView controls.

    If when you go to get the master (in this case Customers) from the Orders via the Orders BindingSource as shown below where there is a customer else there would be no order row.

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.OrdersTableAdapter.Fill(Me.NorthWindAzureDataSet.Orders)
            Me.CustomersTableAdapter.Fill(Me.NorthWindAzureDataSet.Customers)
        End Sub
    
        Private Sub CustomersBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) _
            Handles CustomersBindingNavigatorSaveItem.Click
    
            Me.Validate()
            Me.CustomersBindingSource.EndEdit()
            Me.TableAdapterManager.UpdateAll(Me.NorthWindAzureDataSet)
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            MessageBox.Show(
                CType(OrdersBindingSource.Current, DataRowView).
                               Row.GetParentRow("FK_Orders_Customers2").
                               Field(Of Integer)("CustomerIdentifier").ToString())
    
    
        End Sub
    End Class

    I can valid GetParentRow is working via the MessageBox e.g.

    Note I left the CustomerIdentifier column visible so you can see this works.

    Since this is not working for you

    • You didn't drag-drop the DataGridView controls on the form correctly
    • The relationship is bad (can be many reasons but this goes back to creating the relationship in the database and not in Visual Studio).

    Hope this helps.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Wednesday, December 12, 2018 3:38 AM
    Moderator
  • Hello Ryan,

    Just checking in to see if the issue has been resolved or not?


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, December 13, 2018 5:05 PM
    Moderator