-
Jan 14th, 2021, 12:35 PM
#1
Thread Starter
New Member
[RESOLVED] For / Next Loop fails on last XML node
I'm creating an asp.net application using Visual Studio 2012. I'm using vb.net on back-end. I'm using a SQL Sever 2018 database and the SQL Server database is housed on the same machine that I'm using to develop the application. I'm working on a Windows 10 desktop and my browser is Microsoft Edge. The application I'm creating will be used to track hunting and fishing information. I have created a modal-popup to enter the information related to the trips. When the web application opens it retrieves all the entries I made from the SQL Server database. On the main webform, I'm using a web-control Treeview to filter through the records and I populate the treeview when the app opens dynamically from the SQL database except for a few childnodes from an XML file. I flag each hunting and fishing trip by selecting a yearly "Season" and those seasons appear within the Treeview control. I'm trying to add childnodes under the "Season" indicator and I have created a XML file to store those childnodes. When I loop through the XML nodes I receive the following error. The error appears on the last XML node. ("Index must be within the bounds of the List. Parameter name: index"). Below is the vb.net code I have created to populate the Treeview control. The issue occurs on the last For / Next loop. I also included my XML data below.
HTML Code:
<?xml version="1.0" encoding="utf-8" ?>
<!--XML Application.-->
<HUNT>
<TRIP>
<GAME>Hunting Trip</GAME>
<GAME>Fishing Trip</GAME>
</TRIP>
</HUNT>
Code:
Private Sub PopulateTreeView()
Dim billTypeDA As New billTypeDB
Dim billTypeREG As New billTypeByRegion
Dim taxYearDA As New taxYearTV
Dim billTypes As New utilityBillTypeClass
Dim regionNames As New stateRegionClass
Dim years As New taxYearClass
Dim season As New huntSeasonDB
Dim eTree As TreeView
' create major tree parent
Dim tn As New TreeNode("Hunting/Fishing Details")
Dim sNewTag As String
Try
eTree = Me.huntTreeView
eTree.Nodes.Add(tn)
' add sub-parent to select entry form
Dim entryNode = New TreeNode("Hunt Entry".ToString)
' add Hunt Entry node to tree
tn.ChildNodes.Add(entryNode)
' create child node variable
Dim eNode As New TreeNode
' assign text to variable
eNode.Text = "Hunt Entry Form"
eNode.Target = "dailyHuntEntry.aspx"
' add text to tree
entryNode.ChildNodes.Add(eNode)
' add sub-parent to select hunting seasons
Dim cNode = New TreeNode("Season Hunted/Fished".ToString)
tn.ChildNodes.Add(cNode)
Dim cTable As New DataTable
' set variable to reference temporary data table
' temporary data table is created in the entityChildDB class
' and passed to this event
' this is for the server data table
Dim yw As DataRow
Dim cList As String
cTable = season.GetSeasons(_sValue, _databaseValue)
Dim yCount = cTable.Rows.Count
Dim cCount = cTable.Columns.Count
For Each yw In cTable.Rows
If yCount > 0 Then
Dim yNode As New TreeNode
sNewTag = "_2"
'yNode.Tag = sNewTag
years.taxYear = yw.Item(1)
' add year name
cList = years.taxYear
yNode.Text = cList
cNode.ChildNodes.Add(yNode)
' add sub-children to treeview below hunt season
Dim doc As New XmlDocument()
'' retrieve combobox items from XML file huntFish.xml
doc.Load("I:\Glenn\VB_Apps\huntingWebApplication_v1\huntingWebApplication_v1\XML\huntFish.xml")
'' loop through game node and apply childnodes to treeview
Dim tNode As New TreeNode
Dim XMLnode As XmlNodeList
Dim i As Integer
XMLnode = doc.GetElementsByTagName("GAME")
For i = 0 To XMLnode.Count - 1
tNode.Text = XMLnode(i).ChildNodes(0).Value.ToString
yNode.ChildNodes.Add(tNode)
Next
End If
Next
Catch ex As Exception
' the catch block where exception will be handled
Dim script As String = ex.Message
Page.ClientScript.RegisterStartupScript(Me.GetType(), "alert", "<script language=javascript>alert('" & script & "');</script>", False)
End Try
End Sub
-
Jan 14th, 2021, 12:38 PM
#2
Re: For / Next Loop fails on last XML node
Just a heads up, I wrapped your code in [code][/code] tags. They help format your code. For more information you can visit the link in my signature.
-
Jan 18th, 2021, 01:20 PM
#3
Thread Starter
New Member
Re: [RESOLVED] For / Next Loop fails on last XML node
Resolved the issue by moving the declaration "Dim tNode as New TreeNode" within the FOR/NEXT Loop
Code:
For i = 0 To XMLnode.Count - 1
Dim tNode As New TreeNode
tNode.Text = XMLnode(i).ChildNodes(0).Value.ToString
yNode.ChildNodes.Add(tNode)
Next i
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|