Solved

For loop producing IndexOutOfRange error

Posted on 2014-01-04
7
315 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 35

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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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 35

Accepted Solution

by:
Miguel Oz earned 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
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…

863 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

26 Experts available now in Live!

Get 1:1 Help Now