VB WinForm: No Row at Position 0

Posted on 2007-07-30
Last Modified: 2013-11-26
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)
        Next CampaignRow
        'Output a text file with the complete string
    End Sub

DA.xxxx are calls to a data access class which handles the this case getting the data sets by running a stored procedure, with parameters passed by the class.
Question by:macecase
    LVL 11

    Expert Comment


    whats the full error message reported?

    LVL 34

    Accepted Solution

    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.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    In my previous two articles we discussed Binary Serialization ( and XML Serialization ( In this article we will try to know more about SOAP (Simple Object Acces…
    For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK ( for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now