Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 15223

VS 2012 For / Next Loop fails on last XML node

$
0
0
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


Viewing all articles
Browse latest Browse all 15223

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>