Solved

VB.net Index was out of range?

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

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.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

777 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