Solved

VB.net Index was out of range?

Posted on 2015-01-15
4
311 Views
Last Modified: 2015-01-16
Hey guys,

I can't figure out why I am getting this error? Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: Index VS2010 VB.net

      Dim RC As Integer = dgv_POSData.RowCount
        Dim i As Integer = 0
        txt_Log.Text = txt_Log.Text & "OpenDates " & lbl_SrchStartDate.Text & " and " & lbl_SrchEndDate.Text & vbCrLf
        txt_Log.Text = txt_Log.Text & "Verifying " & RC & " rows" & vbCrLf

        Do While i <= RC
            'MessageBox.Show(i)

            Dim HN As String = dgv_POSData.Rows(i).Cells(2).Value
            Dim DN As String = dgv_POSData.Rows(i).Cells(3).Value
            Dim HF As String = dgv_POSData.Rows(i).Cells(4).Value
            Dim HPF As String = dgv_POSData.Rows(i).Cells(5).Value

            If HN - DN = 0 Then
                txt_Log.Text = txt_Log.Text & "Trans " & dgv_POSData.Rows(i).Cells(1).Value & " Header vs Detail = Good" & vbCrLf
            Else
                Dim RD As Double = HN - DN
                txt_Log.Text = txt_Log.Text & "Trans " & dgv_POSData.Rows(i).Cells(1).Value & " Header vs Detail difference of " & RD & vbCrLf
            End If

            If HF - HPF = 0 Then
                txt_Log.Text = txt_Log.Text & "Trans " & dgv_POSData.Rows(i).Cells(1).Value & " Header vs HowPaid = Good" & vbCrLf
            Else
                Dim RD As Double = HF - HPF
                txt_Log.Text = txt_Log.Text & "Trans " & dgv_POSData.Rows(i).Cells(1).Value & " Header vs HowPaid difference of " & RD & vbCrLf
            End If


            i = i + 1
        Loop

Open in new window



For testing I added a messagebox to show me the value of i. If I uncomment MessageBox.Show(i) it works and fails on the last row of the datagrid, but if I comment it out, it fails on the very first row? I don't understand what displaying the value of i has to do with anything?

What am I missing? Am I going crazy?
0
Comment
Question by:triphen
4 Comments
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 40552728
It looks like you are off by one. RC is the total row count and index starts at 0 and not 1 so you should iterate to 1 less then RC as shown below.

Do While i < RC
0
 

Author Comment

by:triphen
ID: 40552772
That may have worked, but now I have another problem with my SQL it looks like be right back :)
0
 
LVL 8

Expert Comment

by:Naitik Gamit
ID: 40552868
0
 
LVL 33

Expert Comment

by:it_saige
ID: 40554021
*No points*

Just to illustrate what Fernando has stated.  Say I have a collection (or an array) of 5 items.

The count property will return 5.
But the items in the collection/array are accessible by their index.
Arr[0]
Arr[1]
Arr[2]
Arr[3]
Arr[4]

Five items but no index for 5.  So when iterating over a collection based on the count you want to use either count - 1; e.g. -
Module Module1
	Sub Main()
		Dim Arr(4) As Integer
		For i As Integer = 0 To 4
			Arr(i) = i
		Next

		Console.WriteLine("Array Count is {1};{0}Array Length is {2};{0}Array Lower Bounds is {3};{0}Array Upper Bounds is {4};", Environment.NewLine, Arr.Count, Arr.Length, Arr.GetLowerBound(0), Arr.GetUpperBound(0))
		Dim j As Integer = 0
		While (j <= Arr.Count - 1)
			Console.WriteLine("Array Index {0}; contains {1}", j, Arr(j))
			j = j + 1
		End While
		Console.ReadLine()
	End Sub
End Module

Open in new window

Or the count explicitly -
Module Module1
	Sub Main()
		Dim Arr(4) As Integer
		For i As Integer = 0 To 4
			Arr(i) = i
		Next

		Console.WriteLine("Array Count is {1};{0}Array Length is {2};{0}Array Lower Bounds is {3};{0}Array Upper Bounds is {4};", Environment.NewLine, Arr.Count, Arr.Length, Arr.GetLowerBound(0), Arr.GetUpperBound(0))
		Dim j As Integer = 0
		While (j < Arr.Count)
			Console.WriteLine("Array Index {0}; contains {1}", j, Arr(j))
			j = j + 1
		End While
		Console.ReadLine()
	End Sub
End Module

Open in new window

Both of the code examples above produce the following output -Capture.JPG-saige-
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Toolbar to format all windows forms controls at design time needed 5 25
Problem to Office 1 40
orderby list (from Json) 1 28
Nested forach loop to linq 3 24
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

735 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