Solved

For loop producing IndexOutOfRange error

Posted on 2014-01-04
7
320 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Send SMS from vb.net desktop app 30 51
Convert VB web project to C# 3 66
Format Transaction Number 19 49
How do I "share" on social sites? 2 38
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

732 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