TreeView Select Problem

millenniasp

Regular
Joined
Nov 12, 2003
Messages
67
I have a treeview in my ASP.NET application. Here is the code to populate the tree

Private Sub PopulateTree()

If Not IsPostBack Then
Dim strConnectionString As String = "Server=localhost;uid=sa;password=;database=CrystalDevelopment"

Dim strSQLRoot As String = "SELECT DISTINCT(GroupName) FROM CrystalReports"

Dim strSQLChild As String = "SELECT GroupName,ReportName FROM CrystalReports"


Dim cnn As SqlConnection
Dim cmRoot As SqlDataAdapter
Dim cmChild As SqlDataAdapter
Dim ds As DataSet
Dim rowRoot As DataRow
Dim rowChild As DataRow

cnn = New SqlConnection(strConnectionString)
ds = New DataSet
cnn.Open()

cmRoot = New SqlDataAdapter(strSQLRoot, cnn)
cmRoot.Fill(ds, "Root")

cmChild = New SqlDataAdapter(strSQLChild, cnn)
cmChild.Fill(ds, "Child")

ds.Relations.Add("RootChild", _
ds.Tables("Root").Columns("GroupName"), _
ds.Tables("Child").Columns("GroupName"))

'Populate the TreeView from the DataSet.
For Each rowRoot In ds.Tables("Root").Rows
nodeRoot = New TreeNode
nodeRoot.Text = rowRoot("GroupName")

TreeView1.Nodes.Add(nodeRoot)
For Each rowChild In rowRoot.GetChildRows("RootChild")
nodeChild = New TreeNode
nodeChild.Text = rowChild("ReportName")
nodeRoot.Nodes.Add(nodeChild)
Next
Next

'Clean up.
ds.Dispose()
cmRoot.Dispose()
cmChild.Dispose()
cnn.Close()
cnn.Dispose()
End If

that works fine. What I need now is when someone selects a node on the treeview, to capture the name of node. I need this to pass on to a query

Dim myNodeName As String
Dim myReport As String
Dim strConnectionString As String
Dim strSQLReport As String

myNodeName =This is where I am having the problem!!!! How do I set the node name?


strConnectionString = "Server=localhost;uid=sa;password=;database=CrystalDevelopment"

strSQLReport = "SELECT FullReportName FROM CrystalReports WHERE ReportName = " & myNodeName

myda = New SqlDataAdapter(strSQLReport, strConnectionString)
ds = New DataSet
myda.Fill(ds, "ReportName")

myReport = ds.Tables("ReportName").Columns("FullReportName").ToString

ExportReport(myReport)

Any and all help would be greatly appreciated!!!!
 
I'm assuming this TreeView control is a 3rd-party component,
because it's not in the Framework anywhere. There's probably a
SelectedNode and/or SelectedNodeIndex property somewhere in
there that you can retrieve the value of.

We can't help if we don't know the control, however, so do you
have info on who makes it or a link to their site?
 
Ah, very cool. I didn't know about those controls. (for anyone else
interested, click here).

To get the selected node's value, try:
Visual Basic:
myNodeName = TreeView1.Nodes(TreeView1.SelectedNodeIndex).Text
 
YAY! Thank you Thank You Thank YOU!!!

ummm I guess you can gather that it worked ;)

in my confusion yesterday, I had added code that wasn't needed. I changed the following code

Dim myNodeName As String
Dim myReport As String
Dim strConnectionString As String
Dim strSQLReport As String

myNodeName =This is where I am having the problem!!!! How do I set the node name?


strConnectionString = "Server=localhost;uid=sa;password=;database=CrystalDevelopment"

strSQLReport = "SELECT FullReportName FROM CrystalReports WHERE ReportName = " & myNodeName

myda = New SqlDataAdapter(strSQLReport, strConnectionString)
ds = New DataSet
myda.Fill(ds, "ReportName")

myReport = ds.Tables("ReportName").Columns("FullReportName").ToString

ExportReport(myReport)


to

Dim myNodeName As String

myNodeName = TreeView1.Nodes(TreeView1.SelectedNodeIndex).Text

ExportReport(myNodeName)


ExportReport function already took care of looking up the report from the file!
 
actually I spoke too soon! :( The above code only gave me the value of the parent Node. here is the code to get the selected node ( I needed the child node)

Here is the completed code

Dim myReport As String
Dim strConnectionString As String
Dim strSQLReport As String
Dim myNodeIndex As String
Dim myNodeName As String
Dim myArray As Array

myNodeIndex = TreeView1.SelectedNodeIndex.ToString

myArray = Split(CStr(myNodeIndex), ".")

If UBound(myArray) = 0 Then
Exit Sub
ElseIf UBound(myArray) = 1 Then
myNodeName = TreeView1.Nodes(myArray(0)).Nodes(myArray(1)).Text
myNodeName = LTrim(RTrim(myNodeName))
End If

Dim myConnection As SqlConnection

strConnectionString = "Server=localhost;uid=sa;password=;database=CrystalDevelopment"
strSQLReport = "SELECT FullReportName FROM CrystalReports WHERE ReportName = '" & myNodeName & "'"


myConnection = New SqlConnection(strConnectionString)
myConnection.Open()

Dim myCommand As New SqlCommand(strSQLReport, myConnection)
Dim myReader As SqlDataReader = myCommand.ExecuteReader()

myReport = ""

While myReader.Read
myReport = myReader("FullReportName").ToString
myReport = LTrim(RTrim(myReport))
End While

ExportReport(myReport)
End If
 
same problem

I have same problem too.My treeview elements comes from database .It can have changeable level tree,four.How can I can take selected nodes text.thanks
 
Try....

Dim clickedNode As TreeNode
clickedNode = TreeView1.GetNodeFromIndex(TreeView1.SelectedNodeIndex)
txtArea.Text = clickedNode.Text
 
Back
Top