• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1794
  • Last Modified:

Tried ..Loading TreeView Recursively from Sql Database Tbl Vb.net

  I asked this question earlier and thought it worked.  But I still get an error.    I would greatly appreciate any help....

   Thanks!

    I have saved a treeView in a Table: tblTreeView1:

   TvNodeId             TvParentId       TvNodeText  
       0                           -1                 Root
       1                            0                 Sub1
       2                            1                 Sub1a
       3                            1                 Sub1b
       4                            0                 Sub2
       5                            4                 Sub2a
       6                            4                 Sub2b

-----------------------------------------------------------------------
     I have tried to load back from the table into a TreeView...

    I use a button to call LoadTree...

  Private Sub LoadTree()
        TreeView1.Nodes.Clear()
        LoadRecursive(-1)
    End Sub

  Private Sub LoadRecursive(ByVal xParentNodeID As Integer)
        Dim ConnStr As String
        ConnStr = ModMain.GpDcConnStr

        Dim cn As New SqlClient.SqlConnection(ConnStr)
        Dim strSQL As String

        strSQL = " SELECT * FROM tblTreeView1 "
        strSQL += " WHERE TvParentId = " & Str(xParentNodeID) & ";"

        Dim da As New SqlClient.SqlDataAdapter(strSQL, cn)
        Dim ds As New DataSet()
        da.Fill(ds, "MyTreeView1")
        cn.Close()

        Dim dRow As DataRow
        Dim xNodeText As String
        Dim xNodeID As Integer

        For Each dRow In ds.Tables("MyTreeView1").Rows
            xNodeText = dRow("TvNodeName").ToString
            xNodeID = dRow("TvNodeId")
            Console.WriteLine("ParentId => " & xParentNodeID.ToString & "    xNodeId => " & xNodeID.ToString & "   xNodeText => " & xNodeText)

            If xParentNodeID = -1 Then
                TreeView1.Nodes.Add(xNodeID.ToString, xNodeText)
            Else

                The following line is where I get a Null exception...

                TreeView1.Nodes(xParentNodeID.ToString).Nodes.Add(xNodeID.ToString, xNodeText)   <== Error occured here ==>
            End If
            Call LoadRecursive(xNodeID)
        Next
    End Sub

           ====================================
   Following is the output from the Console.writeline statement...

          ParentId => -1    xNodeId => 0   xNodeText => Root
          ParentId => 0    xNodeId => 1   xNodeText => Sub1
          ParentId => 1    xNodeId => 2   xNodeText => Sub1a

          The program stops here with error "Null ReferenceExceptionWas Unhandled"
0
bulrick
Asked:
bulrick
  • 2
1 Solution
 
junglerover77Commented:
Hi  bulrick,

I do think your error is strange. This error can Only happen when the referenced Node doesn't exist in the Nodes collection...
Oh, I see, that's because the Nodes collection doesn't contain the Nodes belong to it's sub node... Well, I think I would have to change my code. I'll get back to you in several minutes.

0
 
junglerover77Commented:
Please change the code like this:

  Private Sub LoadTree()
        TreeView1.Nodes.Clear()
        LoadRecursive(Nothing)
    End Sub

    Private Sub LoadRecursive(ByVal xParentNode As TreeNode)
        Dim ConnStr As String
        ConnStr = ModMain.GpDcConnStr

        Dim cn As New SqlClient.SqlConnection(ConnStr)
        Dim strSQL As String

        If xParentNode Is Nothing Then
            xParentNode = New TreeNode()
            xParentNode.Tag = "-1"
        End If

        strSQL = " SELECT * FROM tblTreeView1 "
        strSQL += " WHERE TvParentId = " & xParentNode.Tag.ToString & ";"

        Dim da As New SqlClient.SqlDataAdapter(strSQL, cn)
        Dim ds As New DataSet()
        da.Fill(ds, "MyTreeView1")
        cn.Close()

        Dim dRow As DataRow
        Dim xNodeText As String
        Dim xNode As TreeNode

        For Each dRow In ds.Tables("MyTreeView1").Rows
            xNodeText = dRow("TvNodeName").ToString

            If xParentNode.Tag = "-1" Then
                xNode = TreeView1.Nodes.Add(xNodeText)
            Else
                xNode = xParentNode.Nodes.Add(xNodeText)
            End If
            xNode.Tag = dRow("TvNodeId").ToString

            LoadRecursive(xNode)
        Next
    End Sub
0
 
bulrickAuthor Commented:
Thanks again!!!  It now works perfectly.  Have a great day.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now