Click here to Skip to main content
15,894,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hullo Good Guys,
I am using
Window Application using VBNET2008, DataReader and TreeView
I need your help, Please help me.


I am trying to use DATAREADER to fill the TreeView control during Runtime
I am using TreeView controls and having problem doing the coding as I have not done it before.

Listed beow are the SQL String and the result of it. And also the graphic drawing of TreeView display

SQL String :
SELECT OrderID, OrderDate ProductID
FROM Invoices WHERE (CustomerID = 'Chops') Order by OrderID, Orderdate
Result of SQL String

OrderId OrderDate ProdID
10254 11/07/1996 74
10254 11/07/1996 24
10254 11/07/1996 55
10254 13/07/1996 203
10254 13/07/1996 112
10370 13/12/1996 74
10370 03/12/1996 1
10370 03/12/1996 64
10370 03/12/1996 56
10519 28/04/1997 10
10519 28/04/1997 60

TreeView Display Format
Graphic drawing of how it should look like


10254 => Parent node
11/07/1996 =>Child node
74 =======> GrandChild node
24
55
13/07/1996 =>Child node
203 ====> GrandChild node
112
10370 => Parent node

03/12/1996 =>Child node
74 ====> GrandChild node
1
64
56




Listing below are my non working coding

<pre lang="vb">Private Sub btnDisplayTreeView_Click(ByVal sender As System.Object,
      ByVal e As System.EventArgs) Handles btnDisplayTreeView.Click

    ' --variable to determine NEW NODE ---
    Dim intOrderID As Integer = 0
    Dim DteOrderDate As Date = Nothing
    Dim intNode As Integer = 0
    Dim intSubNode As Integer = 1


    Dim strsql As String = Nothing

    strsql &= "Select  OrderID, Convert(varchar(10), OrderDate, 103) as [OrderDate], ProductID "
    strsql &= " From OrderInvoices "
    strsql &= " Where (CustomerID = N'" + strCustId + "' )"
    strsql &= " And ( OrderDate between @sDate and @eDate) "
    strsql &= " Order by OrderId, OrderDate "

   'define data object
    sqlconn = New SqlConnection(connstr)
    sqlcmd = New SqlCommand(strsql, sqlconn)
    sqlcmd.Connection.Open()

    'using sqlcmd parameter
    With sqlcmd
       .CommandText = strsql
       .Parameters.Add("@sDate", SqlDbType.DateTime).Value = strFromDate
       .Parameters.Add("@eDate", SqlDbType.DateTime).Value = strToDate
    End With
    DR = sqlcmd.ExecuteReader()

    With Me.TreeViewInvoice
     .BeginUpdate()

      While (DR.Read())


Posted
Comments
Sergey Alexandrovich Kryukov 9-Feb-11 0:26am    
Always tag you language(s) and project type (ASP.NET, WPF, etc.)
--SA
OriginalGriff 9-Feb-11 3:10am    
So what is your question: all I can see is that you have not posted the bit where you use the data.
TeeLeong 9-Feb-11 4:00am    
Hi OriginalGriff</pre>
Thank you for trying to help me.

I am surprise the I did posted the coding in my original posting. Apparently, the overall coding are too much and it does not display all of it.

Here are the continuation of the coding:

<pre>
<pre lang="sql">
With Me.TreeViewInvoice
.BeginUpdate()

While (DR.Read())
If (DR.Item("OrderID")<> intOrderID) Then
intNode += 0
intOrderID = DR.Item("OrderID")

' ---- Parent ---OrderID
.Nodes.Add(DR.Item("OrderID"))
Else
'---- Child Level ----Order Date
If (DR.Item("OrderDate").ToString <> DteOrderDate.ToShortDateString) Then
DteOrderDate = Date.Parse(DR.Item("OrderDate").ToString)

'--- Child ---
.Nodes(intNode).Nodes.Add(DR.Item("OrderDate")) ' --child
End If
'---- GrandChild Level ----ProductId
.Nodes(intNode).Nodes(intSubNode).Nodes.Add(DR.Item("ProductID"))

End If
End While

End With
DR.Close()
sqlconn.Close()
sqlcmd.Dispose()</pre>

</pre>

Hi OriginalGriff
Thank you for trying to help me.

I am surprise the I did posted the coding in my original posting. Apparently, the overall coding are too much and it does not display all of it.

Here are the continuation of the coding:

<pre lang="sql">
With Me.TreeViewInvoice
 .BeginUpdate()

  While (DR.Read())
  If (DR.Item("OrderID")<> intOrderID) Then
    intNode += 0
    intOrderID = DR.Item("OrderID")

    ' ---- Parent ---OrderID
    .Nodes.Add(DR.Item("OrderID"))  
       Else
           '---- Child Level ----Order Date
      If (DR.Item("OrderDate").ToString <> DteOrderDate.ToShortDateString) Then
DteOrderDate = Date.Parse(DR.Item("OrderDate").ToString)

     '--- Child ---
    .Nodes(intNode).Nodes.Add(DR.Item("OrderDate"))   ' --child
           End If
           '---- GrandChild Level ----ProductId
           .Nodes(intNode).Nodes(intSubNode).Nodes.Add(DR.Item("ProductID"))

    End If
 End While

 End With
     DR.Close()
     sqlconn.Close()
     sqlcmd.Dispose()


 
Share this answer
 
Hullo Friends,
Thank you for your help.

Finally, I have solved the coding problems. I was using Debugging on every alternate coding lines to evaluate the events and noticed that the problems were caused by TreeView Parent Nodes, Child Nodes and Grandchild Nodes indexes.

Here are the working coding I am posting it here to share with other Newbies who have similar problems:

Private Sub FFillTreeViewNode()

Dim intOrderID As Integer = 0
Dim dteOrderDate As String = String.Empty
Dim intCNode As Integer = 0
Dim intGCNode As Integer = 0
Dim bolJustNewParent As Boolean = True

Dim strsql As String
strsql = "Select OrderID, Convert(varchar(10), OrderDate, 103) as [OrderDate], ProductID "
strsql += " From TbleSalesInvoices "
strsql += " Where (CustomerID = 'Chops' )"
strsql += " Order By OrderID, OrderDate "

sqlconn = New SqlConnection(connstr)
sqlcmd = New SqlCommand(strsql, sqlconn)
sqlcmd.Connection.Open()
DR = sqlcmd.ExecuteReader

While (DR.Read())

If (DR.Item("OrderID") <> intOrderID)Then

If bolJustNewParent = True Then
bolJustNewParent = False
Else
intCNode += 1
intGCNode = 0
End If

'--- Parent ---
Me.TreeViewTest.Nodes.Add(DR.Item("OrderID").ToString)
intOrderID = DR.Item("OrderID")

'--- Child ----
Me.TreeViewTest.Nodes(intCNode).Nodes.Add(DR.Item("OrderDate").ToString)
dteOrderDate = DR.Item("OrderDate").ToString
End If

' --- Child ---
If (DR.Item("OrderDate").ToString <> dteOrderDate) Then

Me.TreeViewTest.Nodes(intCNode).Nodes.Add(DR.Item("OrderDate").ToString)
dteOrderDate = DR.Item("OrderDate").ToString
intGCNode += 1

If intOrderID <> DR.Item("OrderID") Then
intCNode += 1
End If

End If
'---grandchild
Me.TreeViewTest.Nodes(intCNode).Nodes(intGCNode).Nodes.Add(DR.Item("ProductID"))
End While

sqlconn.Close()
DR.Close()
sqlcmd.Dispose()
End Sub


Private Sub TreeViewTest_AfterSelect(ByVal sender As System.Object,
ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeViewTest.AfterSelect
Me.txtTVItem.Text = Me.TreeViewTest.SelectedNode.FullPath.ToString
End Sub
 
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