Solved

For loop producing IndexOutOfRange error

Posted on 2014-01-04
7
318 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ejb wildfly example 2 70
Adding a countdown to HTA 12 94
VB.NET Inline If statement 4 39
what Script langage can i run on Online Excel without having the installed version 3 54
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …

740 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