Bind Treeview to SQL

absent

Well-known member
Joined
Sep 25, 2003
Messages
55
Location
UK
I dont know if this thread is in the right forum, but here goes.

I have a Treeview (yes same problem I have addressed recently with ABODB databindings) and I can bind it to a Access database no problems. But in my infinite wisdom and learning vb.net I never really saw the need for SQL until recently... One it makes things more safer and the code is cleaner...

*clears head*

Back to my problem... I have a treeview I want to bind to a field in a SQL Server DB. the code I am using to "TRY" achieve this is as follows

Private Sub BindToTreeSQL()

Dim trvClient As TreeView
Dim varTvItem As TreeNode
Dim varDsRow As DataRow

trvClients.Nodes.Clear()

For Each varDsRow In SqlDsClients.Tables("ClientName").Rows

varTvItem = New TreeNode

With varTvItem
.Text = varDsRow.Item("ClientName")
End With
trvClients.Nodes.Add(varTvItem)
Next

End Sub

I have placed a call in my form load statement calling this sub. and I get an error :

An unhandled exception of type System.NullReferenceException occurred in Time Tracker.exe

Additional information: Object reference not set to an instance of an object.

Can anyone help me either fixing the problem? Or showing me how to bind a treeview to a SQL Database table and field.

Thanx a million
 
Code:
Friend Sub BindToSQL()

Dim i As Integer
        Dim node_letter As String
        trvClients.Nodes.Clear()

        Set document type
        trvClients.Nodes.Add("Client List")

        set documents
        trvClients.SelectedNode = trvClients.Nodes.Item(0)

        trvClients.ExpandAll()
        trvClients.ShowLines = True
        trvClients.ShowPlusMinus = True
        For i = 1 To 26
            node_letter = Chr(64 + i)
            trvClients.SelectedNode.Nodes.Add(node_letter)
        Next

        Treeview Databinding
        Dim myrdr As SqlClient.SqlDataReader
        Dim node_key As String
        Dim entry_letter As String
        Dim node_name As String

        set_NewTree()
        SqlCommand1.Connection.Open()
        myrdr = SqlCommand1.ExecuteReader(CommandBehavior.CloseConnection)
        Do While myrdr.Read
            node_key = myrdr.GetString(1)
            entry_letter = Mid(myrdr.GetString(1), 1, 1)
            Dim MyNode As TreeNode
            i = 0
            node_name = myrdr.GetString(1) & ", " & myrdr.GetString(2)
            For Each MyNode In trvClients.Nodes.Item(0).Nodes
                If MyNode.Text = entry_letter Then
                    trvClients.Nodes.Item(0).Nodes.Item(MyNode.Index).Nodes.Add(node_name)
                End If
            Next
        Loop
        myrdr.Close()
        SqlConnection1.Close()
End Sub
 
Back
Top