Im trying to show the organization structure of a company.
For this, I retrieved data from a table and displayed it using Treeview Control. When a node is clicked, if the employee's are present under the clicked node, then it is retrieved again and shown.
But I would like to have this all at once without clicking the node.
AtFirst I retrieved empid, name from emptable where manager field is null. So the retrieved records might be MD or Seniormost pupil for those only manager field is null.
Then when a node is clicked, the empid,name from emptable where manager=selected node empID and shown as child nodes.
My query is I want to display the empid, name from emptable at once instead of when a node is selected and retrieved in TreeView1_SelectedNodeChanged event.
For more detail, I have given my coding below:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If IsPostBack = False Then
Try
Dim empid As String
Dim Pnode As TreeNode
server_name = Session("snam")
server_version = Session("sver")
dbusername = Session("dbus")
dbpassword = Session("dbpwd")
compny_name = Session("cmpnm")
myConnection = New SqlConnection("Server='" & server_name & "';User Id='" & dbusername & "';Password ='" & dbpassword & "';DataBase='" & compny_name & "';")
MyCommand.Connection = myConnection
MyCommand.CommandText = "Select * from emptable where manager is NULL "
MyCommand.CommandType = CommandType.Text
myDA.SelectCommand = MyCommand
myDA.Fill(myDS, "emptable")
For Each Row As DataRow In myDS.Tables("emptable").Rows
empid = Row("empID")
Pnode = New TreeNode(Row("empID") & "-" & Row("firstName") & "" & Row("lastName"))
TreeView1.Nodes.Add(Pnode)
Next
child(Pnode, empid)
myDS.Tables("emptable").Clear()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If
Public Sub child(ByVal k As TreeNode, ByVal id As String)
Try
myConnection = New SqlConnection("Server='" & server_name & "';User Id='" & dbusername & "';Password ='" & dbpassword & "';DataBase='" & compny_name & "';")
MyCommand.Connection = myConnection
Try
myDS.Tables("emptable").Clear()
Catch ex As Exception
End Try
MyCommand.CommandText = "Select * from emptable where manager='" & id & "'"
MyCommand.CommandType = CommandType.Text
myDA.SelectCommand = MyCommand
myDA.Fill(myDS, "OHEM")
For Each Row As DataRow In myDS.Tables("emptable").Rows
Dim TNode As New TreeNode(Row("empID") & "-" & Row("firstName") & "" & Row("lastName"))
k.ChildNodes.Add(TNode)
Next
myDS.Tables("emptable").Clear()
Catch Excep As Exception
MsgBox(ex.Message)
End Try
End Sub
Protected Sub TreeView1_SelectedNodeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TreeView1.SelectedNodeChanged
Dim cnode As TreeNode
Dim val As String
Dim location As Integer
cnode = TreeView1.SelectedNode
Try
val = TreeView1.SelectedNode.Text.ToString
location = val.IndexOf("-")
val = val.Substring(0, location)
cnode.ChildNodes.Clear()
Catch ex As Exception
Exit Sub
End Try
child(cnode, val)
End Sub
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.