Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

For loop producing IndexOutOfRange error

Posted on 2014-01-04
7
Medium Priority
?
324 Views
Last Modified: 2014-01-06
I keep getting the following error and unsure has to how fix it. As a fairly new user to VB.NET, I think it is saying that there are no rows at that position? To compensate for this, I included an If statement to check the row count, but it is still producing this error. In fact, the messagebox is not firing at all.

Someone suggested that this could be the solution but not sure how to code.

The page size is greater than the number of records in the result set. Use Math.Min to get the lesser of the two values and then use that as the upper limit for your loop

Can someone please advise as to how I can correct this error. Thanks

error:  
There is no row at position 1.
Link where code obtained: http://support.microsoft.com/kb/305271/en-us

Private Sub loadpages()

    Dim i As Integer
    Dim startRec As Integer
    Dim endRec As Integer
    Dim dtTemp As DataTable
    'Dim dr As DataRow

    'Duplicate or clone the source table to create the temporary table.
    dtTemp = dtSource.Clone

    If currentPage = PageCount Then
        endRec = maxRec
    Else
        endRec = pageSize * currentPage
    End If

    startRec = recNo

    'Copy the rows from the source table to fill the temporary table.
    If dtSource.Rows.Count <> 0 Then

        For i = startRec To endRec - 1
            dtTemp.ImportRow(dtSource.Rows(i)) <--- ERROR HERE
            recNo = recNo + 1
        Next
    Else
        MessageBox.Show(dtSource.Rows.Count.ToString())
    End If
    frmMain.DGV.DataSource = dtTemp
    DisplayPageInfo()
    'fillPostings()
End Sub

Open in new window


combobox sub to change pagesize

Sub cmbpage()

        'Set the start and max records. 
        pageSize = CInt(frmMain.cmbPageSize.Text)
        maxRec = dtSource.Rows.Count
        PageCount = maxRec \ pageSize
        MessageBox.Show(CStr(maxRec))
        ' Adjust the page number if the last page contains a partial page.
        If (maxRec Mod pageSize) > 0 Then
            PageCount = PageCount + 1
        End If

        'Initial seeings
        currentPage = 1
        recNo = 0

        ' Display the content of the current page.

        UDGfillPostings()
        loadpages()

    End Sub

Open in new window

0
Comment
Question by:peter-cooper
  • 3
  • 2
  • 2
7 Comments
 
LVL 22

Expert Comment

by:p_davis
ID: 39756609
what if you try

If dtSource.Rows.Count > -1 Then
0
 
LVL 36

Expert Comment

by:Miguel Oz
ID: 39756661
Replace line 21  at load pages sub with: If dtSource.Rows.Count > 0  Then
Reason: Logic should ensure the valid limit. (There are rows in dataset)

Notice that the demo does not  check for empty rows.
Note for further code review (post question):  Your new combo box must not allow page size > dtSource.Rows.Count (It should be catch as error or should default to one page) other wise you will have incorrect values (PageCount  = 0)
0
 
LVL 22

Expert Comment

by:p_davis
ID: 39756666
ah yeah... for some reason i was thinking of index not count. > 0
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:peter-cooper
ID: 39757178
@mas Still the same error. It seems to happen if the count of 'i' is less than the value of the combobox say '25'.
0
 
LVL 36

Accepted Solution

by:
Miguel Oz earned 2000 total points
ID: 39758761
Please prorvide the value of dtSource.Rows.Count when combobox = '25'.
If dtSource.Rows.Count < 25 your current code does not work (combobox cmbpage code) because PageCount  = 0.
As per my previous note comment, you should catch this scenario at sub cmbpage, for example replace line 6 to 11 with :
       If dtSource.Rows.Count <= pageSize Then
            PageCount = 1
        Else
           'Your current code calc PageCount  goes here
           PageCount = maxRec \ pageSize
           If (maxRec Mod pageSize) > 0 Then
              PageCount = PageCount + 1
           End If
       End If

Open in new window

0
 

Author Comment

by:peter-cooper
ID: 39758837
@mas Thanks for that. There are still some issues, but none I don't think I can overcome. If I get stuck, I shall create a new post. Many thanks
0
 

Author Closing Comment

by:peter-cooper
ID: 39758838
Thanks once again.
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
When you discover the power of the R programming language, you are going to wonder how you ever lived without it! Learn why the language merits a place in your programming arsenal.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
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…

972 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