Loading a Treeview in Vb.NET



Private Sub LoadTreeView(ByVal sender As System.Object, ByVal e As System.EventArgs) 

        '# DECLARATIONS 

        Dim strSQL As String '# SQL query 
        Dim drReader As SqlDataReader '# DataReader Object 
        Dim strCurrentNode As String '# Currentlly processed Node(text value) 
        Dim strPreviousNode As String '# Previously processed node(text value) 
        Dim intCount As Integer '# To increment node index if needed 
        Dim intChildCount As Integer '# To count child nodes 
        Dim strChildNode As String '# mnuItem (text value) 
        Dim blnFirstLoop As Boolean '# is first loop ? 
        Dim strLink As String '# mnuLink (text value) 
        Dim intIco As String '# mnuIcon (icon index value) 
        Dim intIconIndex As Integer '# to hold icon index for treeVie 



        '# setting connection 
        Dim cnSqlServer As New SqlConnection(strConnection) 
        Try 

            '# opening the connection 
            cnSqlServer.Open() 
            '# ADMIN'S Access 
            If g_strCurrentUser = "Admin" Then 
                strSQL = "" 
                'This is the list of menu items that the user has access to 
                strSQL = "SELECT tblMenu.* " 
                strSQL = strSQL & "FROM tblMenu " 
                strSQL = strSQL & "ORDER BY tblMenu.mnuGroup DESC, tblMenu.mnuOrder" 
                Dim cmd As New SqlCommand(strSQL, cnSqlServer) 
                drReader = cmd.ExecuteReader 
            Else 
                strSQL = "" 
                'This is the list of menu items that the user has access to 
                strSQL = "SELECT tblUserAccess.UserName, tblMenu.* " 
                strSQL = strSQL & "FROM tblUserAccess, tblMenu " 
                strSQL = strSQL & "WHERE tblUserAccess.UserName ='" & g_strCurrentUser & "' " 
                strSQL = strSQL & "AND tblMenu.mnuAuthorised = 1 " 
                strSQL = strSQL & "AND tblUserAccess.mnuId = tblMenu.mnuId " 
                strSQL = strSQL & "ORDER BY tblMenu.mnuGroup DESC, tblMenu.mnuOrder" 
                Dim cmd As New SqlCommand(strSQL, cnSqlServer) 
                drReader = cmd.ExecuteReader 
            End If 



            '# We will check the current item's group against 
            '# the previous item's group. 
            '# If there is a change, then we add a parent node 
            '# If there is not a change, then we add the item to the child node 
            strPreviousNode = "" 
            intCount = 0 
            intChildCount = 0 
            blnFirstLoop = True 
            Do While drReader.Read 

                '# getting database fields to variables 
                strCurrentNode = Trim(drReader("mnuGroup")) 
                strChildNode = Trim(drReader("mnuItem")) 
                strLink = Trim(drReader("mnuLink")) 
                intIco = Trim(drReader("mnuIcon")) 

                '# reading the mnuItem,mnuLink into array 





                '# skip this section if inside the first loop 
                If blnFirstLoop = False Then 
                    '# if parent node changed increment intCount and get redy 
                    '# for next parent node 
                    If strCurrentNode <> strPreviousNode Then 
                        intCount = intCount + 1 
                    End If 
                End If 
                If strCurrentNode = strPreviousNode Then 
                    '# Add a child node to the existing parent node 
                    '# add CHILD NODE 
                    trvMenu.Nodes(intCount).Nodes.Add(strChildNode) 

                    '# setting the picture for the parent node 
                    trvMenu.Nodes(intCount).ImageIndex = intIco 
                    trvMenu.Nodes(intCount).SelectedImageIndex = intIco 


                    '# add link value to tag property 
                    trvMenu.Nodes(intCount).Nodes(intChildCount).Tag = strLink 

                    '# setting the picture for child node 
                    trvMenu.Nodes(intCount).Nodes(intChildCount).ImageIndex = intIco 
                    trvMenu.Nodes(intCount).Nodes(intChildCount).SelectedImageIndex = intIco 


                Else 
                    blnFirstLoop = False 
                    '# Add a parent node first and then a child node under that 

                    '# add PARENT node 
                    trvMenu.Nodes.Add(strCurrentNode) 

                    '# setting the picture for the parent node 
                    trvMenu.Nodes(intCount).ImageIndex = intIco 
                    trvMenu.Nodes(intCount).SelectedImageIndex = intIco 

                    '# let child count 0 again as starting a new child node 
                    intChildCount = 0 



                    '# add CHILD NODE 
                    trvMenu.Nodes(intCount).Nodes.Add(strChildNode) 
                    '# add link value to tag property 
                    trvMenu.Nodes(intCount).Nodes(intChildCount).Tag = strLink 
                    '# setting the picture for child node 
                    trvMenu.Nodes(intCount).Nodes(intChildCount).ImageIndex = intIco 
                    trvMenu.Nodes(intCount).Nodes(intChildCount).SelectedImageIndex = intIco 



                End If 

                '# Make the current node the previous node 
                strPreviousNode = strCurrentNode 
                '# Increment child node count 
                intChildCount = intChildCount + 1 

            Loop 

        Catch ex As Exception 
            MsgBox("frmMain, LoadTreeView : " & ex.Message) 
            MsgBox(ex.ToString) 
        End Try 



        '# openning the treeview 
        trvMenu.ExpandAll() 

    End Sub