Solved

VB.net Index was out of range?

Posted on 2015-01-15
4
337 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
[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
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 34

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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Suggested Solutions

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

739 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