Link to home
Start Free TrialLog in
Avatar of bulrick
bulrick

asked on

Saving TreeView to Database in vb.net recursively


I am struggling with saving a TreeView structure to a SQL database.

Essentially I am adding Nodes to a tree at runtime.  When I Click a button
I want to save the TreeView in a database.  (Below is an example of
showing treeView create in code)

I have converted some C -code to Vb I found in Experts Exchange but am still
having problems.   Maybe I have been staring at this too long!!!!  
I urgently need to get this resolved....

I will greatly appreciate help!!!

-----------------------------------------------------------
TreeView created at runtime in code using Context Menu
with Add/Delete options
------------------------
  Add Node code:
 
    Private Sub AddNode_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim xChild As New TreeNode
        Dim xParent As String
        xChild.Text = Me.txtNodeName.Text
        LastNode += 1
        xChild.Tag = LastNode
        TreeView1.SelectedNode.Nodes.Add(xChild)
     End Sub

---------------------------
Root
  Sub1
    Sub1a
  Sub2
    Sub2a
      Sub2b
      
'==================================================================================================
    Private Sub SaveTree(ByVal TreeView1 As TreeView)
        Dim xNode As TreeNode
        Dim NumNodes = 0
        Dim xNodeName As String
        Call Delete_AllRecord_tblTreeView1()
        For Each xNode In TreeView1.Nodes
            xNodeName = xNode.Text
            NumNodes += 1
            Call SaveRecursive(xNode)
        Next
        MsgBox("Number of Nodes =" & Str(NumNodes), MsgBoxStyle.Information, "Total Number of Nodes Saved")
    End Sub      
      
  Private Sub SaveRecursive(ByVal Node As TreeNode)
        Dim MyId As Integer = Node.Tag
        Dim ParentId As New Integer
        Dim MyName As String = Node.Text
        Dim strSQL As String
        Dim ix As Integer
        Console.WriteLine("MyId = " & MyId)
            
    '??????????????????????????????????
   '   I this is where I am getting an error....
   '   I am hopeful that if I can get this working the structure will be saved in my database.
            

        If Not Node.Parent Is Nothing Then
           ParentId = Int(Node.Parent.Tag)     '===> Error saying New Instance of Object required
        End If
            
    '???????????????????????????????????

        strSQL = " INSERT INTO tblTreeView1 "
        strSQL += "(TvNodeId, TvParentId, TvNodeName, TvPermission) "
        strSQL += " VALUES ( "
        strSQL += Str(MyId) & ", "
        strSQL += Str(ParentId) & ", "
        strSQL += "'" & MyName & "'" & ", "
        strSQL += "0" & ");"

        '==> Execute SQL Query

        Dim xNumAffected As Int32
        Dim GpDcConnStr As String
        GpDcConnStr = ModMain.GpDcConnStr

        Dim cn As New SqlClient.SqlConnection(GpDcConnStr)
        cn.Open()
        Try
            Dim cmd As New SqlClient.SqlCommand(strSQL, cn)
            ' debug ..Console.WriteLine("strsql ==>" & strSQL & "<==")
            xNumAffected = cmd.ExecuteNonQuery()
        Catch ex As Exception
            MsgBox("Error (111) Cannot Append TreeNodes to DataBase", MsgBoxStyle.Exclamation, "Database Error")
        End Try
        cn.Close()

        Dim child As TreeNode
        For Each child In Node.Nodes
            Call SaveRecursive(child)
        Next
    End Sub
      

----------------------------------------------------------
By commenting out the lines of code in question the database
fields show as follows:  
 
TreeView in Database:

TvNodeId     TvParentId     TvNodeName  TvPermission      
  0                  0              Root           0
  1                        0              Sub1           0
  3                        0              Sub1a         0
  2                        0              Sub2           0
  4                        0              Sub2a         0
  5                        0              Sub2b         0
ASKER CERTIFIED SOLUTION
Avatar of ctm5
ctm5

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
As ctm5 pointed out - the new could be the issue

another way is to send it the parentId in each recursive call

Private Sub SaveRecursive(ByVal Node As TreeNode, ByVal ParentId As Integer)

' Take out the if statement

' In your function
SaveRecursive(child, MyId)



' In your main procedure
SaveRecursive(xNode, 0)   ' Where 0 as a parentID defines that it is a root node


HTH
~BC








Avatar of bulrick
bulrick

ASKER

Thank you.... removing the 'New' solved the problem....  

    Somehow I was overlooked the small stuff.... but... finally I can move on...

   thanks!!!!!!!!!!