Click here to Skip to main content
15,892,517 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a access database with two tables namely Tblreqs and Tbllib, I used Tblreqs to populate into treeview, i want to use the selected node to fill textboxes with data from Tbllib.
Here are the codes have tried so far 


What I have tried:

Imports System.Data.OleDb
Public Class Form1

    Private Sub TblreqsBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs)
        Me.Validate()
        Me.TblreqsBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.CopycopyDataSet)

    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'CopycopyDataSet.tbllib' table. You can move, or remove it, as needed.
        Me.TbllibTableAdapter.Fill(Me.CopycopyDataSet.tbllib)
        'TODO: This line of code loads data into the 'CopycopyDataSet.tbllib' table. You can move, or remove it, as needed.
        Me.TbllibTableAdapter.Fill(Me.CopycopyDataSet.tbllib)
        'TODO: This line of code loads data into the 'CopycopyDataSet.tbllib' table. You can move, or remove it, as needed.
        Me.TbllibTableAdapter.Fill(Me.CopycopyDataSet.tbllib)
        'TODO: This line of code loads data into the 'CopycopyDataSet.tblreqs' table. You can move, or remove it, as needed.
        Me.TblreqsTableAdapter.Fill(Me.CopycopyDataSet.tblreqs)

        GroupTextBox.Text = " "
        DetailsTextBox.Text = " "
        RefnoTextBox.Text = " "
        QtyTextBox.Text = " "
        RateTextBox.Text = " "
        UnitTextBox.Text = " "
        TotalTextBox.Text = " "

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        TblreqsTableAdapter.Connection.Open()
        TreeView1.Nodes.Clear()
        FillTree("1", "Library", Nothing)
        TblreqsTableAdapter.Connection.Close()
    End Sub

    Public Sub FillTree(ByVal Key As String, ByVal Txt As String, ByVal N As TreeNode)
        Dim cn As OleDbConnection
        Dim cmd As OleDbCommand
        Dim NN As TreeNode
        If N Is Nothing Then
            NN = TreeView1.Nodes.Add(Key, Txt)
        Else
            NN = N.Nodes.Add(Key, Txt)
        End If
        cn = TblreqsTableAdapter.Connection
        cmd = New OleDbCommand("select * from tblreqs where idparent='" & Key & "'", cn)
        Dim dr = cmd.ExecuteReader
        Do While dr.Read()
            FillTree(dr("id"), dr("detail"), NN)
        Loop
        dr.Close()
        cmd.Dispose()
    End Sub

    Private Sub TreeView1_AfterSelect(sender As Object, e As TreeViewEventArgs) Handles TreeView1.AfterSelect

        Dim cn As OleDbConnection
        Dim wybrany As String
        wybrany = e.Node.Text
        cn = TblreqsTableAdapter.Connection
        Dim cmd As New OleDbCommand
        Dim dat As OleDbDataReader
        Dim querry As String = "select Refno, Group, Details, Qty, Rate, Total, Unit from tbllib where (detail) = '" + wybrany + "'"
        cmd = New OleDbCommand(querry, cn)
        dat = cmd.ExecuteReader
        While dat.Read()

            RefnoTextBox.Text = dat.Item(0).ToString
            GroupTextBox.Text = dat.Item(1).ToString
            DetailsTextBox.Text = dat.Item(2).ToString
            QtyTextBox.Text = dat.Item(3).ToString
            RateTextBox.Text = dat.Item(4).ToString
            TotalTextBox.Text = dat.Item(5).ToString
            UnitTextBox.Text = dat.Item(6).ToString

        End While
    End Sub

End Class
Posted
Updated 20-Sep-20 1:20am

1 solution

I don't see where have you used SelectedNode property.

Have a look at the details here: TreeView.SelectedNode Property (System.Windows.Forms) | Microsoft Docs[^]

Sample:
VB
Private Sub myButton_Click(ByVal sender As Object, _
  ByVal e As System.EventArgs) Handles myButton.Click
   ' Set the tree view's PathSeparator property.
   myTreeView.PathSeparator = "."

   ' Get the count of the child tree nodes contained in the SelectedNode.
   Dim myNodeCount As Integer = myTreeView.SelectedNode.GetNodeCount(True)
   Dim myChildPercentage As Decimal = CDec(myNodeCount) / _
      CDec(myTreeView.GetNodeCount(True)) * 100

   ' Display the tree node path and the number of child nodes it and the tree view have.
   MessageBox.Show(("The '" + myTreeView.SelectedNode.FullPath + "' node has " _
      + myNodeCount.ToString() + " child nodes." + Microsoft.VisualBasic.ControlChars.Lf _
      + "That is " + String.Format("{0:###.##}", myChildPercentage) _
      + "% of the total tree nodes in the tree view control."))
End Sub

Like above, once you know your selected node, use the way you seek (put in textbox as you shared)


Now, coming to a critical thing, your code is open for SQL Injection. You should use parametrized query for accessing data. Read about it:
SQL Injection[^]
Accessing data with ADO.NET[^]
How to: Perform Parameterized Queries - SQL Server | Microsoft Docs[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900