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
Solved

For loop producing IndexOutOfRange error

Posted on 2014-01-04
7
317 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.

828 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