Solved

VB.net Index was out of range?

Posted on 2015-01-15
4
214 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 62

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 32

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

758 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

18 Experts available now in Live!

Get 1:1 Help Now