trouble with a treeview add

B

brad langhorst

I'm going over a result set and adding results as levels on
the tree control.

when stepping through the code i see execution switch to
the calling subroutine as soon as the parentnode.nodes.add
is called. Seems like i'm trampling some variable (btw:
why doesn't that raise an error?)

I'm a fairly experienced programmer but i'm new to vb.net
so maybe this is something trivial...

here's my code

While rdrCCOA.Read
'results must be ordered by coalinekey and name to work

levelname = CStr(rdrCCOA.GetValue(0))
levelpart = CStr(rdrCCOA.GetValue(1))
level = CInt(rdrCCOA.GetValue(2))
coalinekey = CInt(rdrCCOA.GetValue(3))
acctno = CStr(rdrCCOA.GetValue(4))

'keep adding until coalinekey changes
Select Case level
Case 1 'create new root node if this one is not
already there
parentNode = findNode(acctTree, levelname)
If (IsNothing(parentNode)) Then 'not already
present add it
parentNode = New TreeNode(levelname)
acctTree.Nodes.Add(parentNode)
End If

Case Else 'add below that node
If IsNothing(parentNode) Then Throw New
Exception("deep node found with no parent")
parentNode = findNode(acctTree, levelname)
If IsNothing(parentNode) Then 'not already
present, add it
newNode = New TreeNode(levelname)
parentNode.Nodes.Add(newNode)
parentNode = newNode
End If
End Select
End While
 
A

Antoine Cote [MSFT]

Hello Brad,

when the code steps out suddently like this, it often indicates that an exception occured. You could try to run your program under the debugger and turn on
the "Break on exceptions" feature. Here's how to do it once VS is in debug mode:

1. Go to the Debug/Exceptions... menu
2. Click on the "Common Language Runtime Exceptions" item.
3. Set the option for "When an exception is thrown" at the bottom of the dialog to "Break into the debugger"
4. Execute the line of code in question (parentNode.Nodes.Add)

In your example below you do the following:
parentNode = findNode(acctTree, levelname)
If IsNothing(parentNode) Then 'not already
present, add it
newNode = New TreeNode(levelname)
parentNode.Nodes.Add(newNode)
parentNode = newNode
End If

If the findNode method returns Nothing then IsNothing will return True and you will end up calling parentNode.Nodes with an invalid reference. So the
problem you're having might be caused by an 'Object reference not set' exception.

HTH

Antoine
Microsoft Visual Basic .NET

--------------------
Content-Class: urn:content-classes:message
From: "brad langhorst" <[email protected]>
Sender: "brad langhorst" <[email protected]>
Subject: trouble with a treeview add
Date: Sun, 24 Aug 2003 22:15:23 -0700
Lines: 45
Message-ID: <[email protected]>
MIME-Version: 1.0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
X-Newsreader: Microsoft CDO for Windows 2000
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
Thread-Index: AcNqx+MPmdQcRuE6QE+ymSh95WPH+A==
Newsgroups: microsoft.public.dotnet.languages.vb
Path: cpmsftngxa06.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vb:130143
NNTP-Posting-Host: TK2MSFTNGXA06 10.40.1.53
X-Tomcat-NG: microsoft.public.dotnet.languages.vb

I'm going over a result set and adding results as levels on
the tree control.

when stepping through the code i see execution switch to
the calling subroutine as soon as the parentnode.nodes.add
is called. Seems like i'm trampling some variable (btw:
why doesn't that raise an error?)

I'm a fairly experienced programmer but i'm new to vb.net
so maybe this is something trivial...

here's my code

While rdrCCOA.Read
'results must be ordered by coalinekey and name to work

levelname = CStr(rdrCCOA.GetValue(0))
levelpart = CStr(rdrCCOA.GetValue(1))
level = CInt(rdrCCOA.GetValue(2))
coalinekey = CInt(rdrCCOA.GetValue(3))
acctno = CStr(rdrCCOA.GetValue(4))

'keep adding until coalinekey changes
Select Case level
Case 1 'create new root node if this one is not
already there
parentNode = findNode(acctTree, levelname)
If (IsNothing(parentNode)) Then 'not already
present add it
parentNode = New TreeNode(levelname)
acctTree.Nodes.Add(parentNode)
End If

Case Else 'add below that node
If IsNothing(parentNode) Then Throw New
Exception("deep node found with no parent")
parentNode = findNode(acctTree, levelname)
If IsNothing(parentNode) Then 'not already
present, add it
newNode = New TreeNode(levelname)
parentNode.Nodes.Add(newNode)
parentNode = newNode
End If
End Select
End While


--

This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this message are best directed to the newsgroup/thread from which they originated.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top