VB WinForm: No Row at Position 0

I am authoring a VB.NET application, using VS 2003,  which builds an html string based on records from a SQL table, then spins through the record set, acceses records from a child table, appends the html string, spins through that table's child table, appends the html string, then goes to the next parent, and so forth. In other words, I have a nested record loop...going through three sets of records.  The final output is a formatted block of html code that will be pasted into a web page via a content editor. There are reasons for this approach that I won't go into here. When I execute the code, it goes all the way through the first record (parent, child and grandchild), and then gives me  an error "There is no row at position 1."

Following is the code for this routine:

    Private Sub btnHTML_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHTML.Click
        Dim x As Integer = 0
        Dim y As Integer = 0
        Dim z As Integer = 0

        Dim strCampaignBlock As String
        Dim strPromoBlock As String
        Dim strLimitBlock As String

        Dim strImagePath As String
        Dim strPromoDescription As String

        Dim HTMLTableHeader As String = "<html><table border=""0"" cellspacing=""0"" cellpadding=""0"" width=""385""><tr>"
        Dim HTMLTableFooter As String = "</table>"
        Dim strListEnd As String = "</ul>"

        Dim dtCampaigns As New DataTable
        Dim dtPromos As New DataTable
        Dim dtLimits As New DataTable

        Dim CampaignRow As DataRow
        Dim PromoRow As DataRow
        Dim LimitRow As DataRow

        dtCampaigns = DA.RunSPGetDS("SelectHTMLCampaigns").Tables("HTMLCampaigns")

        For Each CampaignRow In dtCampaigns.Rows
            strCampaignBlock = dtCampaigns.Rows(x).Item("Header1") + dtCampaigns.Rows(x).Item("Header2")

            dtPromos = DA.RunSPGetDS("SelectPromosHTML", _
                DA.CreateParam("@CampaignID", dtCampaigns.Rows(x).Item("MCCampaignID"))).Tables("PromosHTML")

            x = x + 1

            For Each PromoRow In dtPromos.Rows
                If dtPromos.Rows.Count >= 1 Then
                    strPromoBlock = dtPromos.Rows(y).Item("FormattedPromo")

                    dtLimits = DA.RunSPGetDS("SelectHTMLLimits", DA.CreateParam("@PromoID", dtPromos.Rows(y).Item("PromoID"))).Tables("HTMLLimits")

                    If dtLimits.Rows.Count >= 1 Then
                        For Each LimitRow In dtLimits.Rows
                            strLimitBlock = strLimitBlock + dtLimits.Rows(z).Item("FormattedLimit")
                            z = z + 1
                            MessageBox.Show(strLimitBlock, "in limit loop")
                        Next LimitRow
                    End If
                    strPromoBlock = strPromoBlock + strLimitBlock
                    y = y + 1
                    strLimitBlock = ""
                End If

                strCampaignBlock = strCampaignBlock + strPromoBlock + strListEnd

            Next PromoRow

            Debug.WriteLine(HTMLTableHeader + strCampaignBlock _
                + HTMLTableFooter)
            MessageBox.Show("waiting")
        Next CampaignRow
        'Output a text file with the complete string
    End Sub

DA.xxxx are calls to a data access class which handles the ADO...in this case getting the data sets by running a stored procedure, with parameters passed by the class.
macecaseAsked:
Who is Participating?
 
SanclerCommented:
You are not resetting y to 0 on each fresh run through the outer loop or z to 0 on each run through the inner loop.

Roger
0
 
Babycorn-StarfishCommented:
Hi,

whats the full error message reported?

thx
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.