Error : System.ArgumentOutOfRangeException: Index was out of range.

kookaburra
08-02-2004, 12:30 AM
This code works if I select only the first one or two items, but most of the time throws and exception "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index". Please tell me what I've done wrong....


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

If Not Page.IsPostBack Then
Dim ex() As String = New String() {"ADE", "BRI", "MEL", "CAN", "CEN", "DAR", "GDC", "HOB", "LAU", "N01", "N02", "N03", "N04", "N05", "N06", "NCL", "PER", "Q01", "Q02", "Q03", "Q04", "Q05", "Q06", "Q07", "Q08", "S02", "S03", "S04", "Kang Is", "SUN", "SYD", "T02", "TSV", "V01", "V02", "V03", "W01", "W02", "W03", "W04", "W05", "W06", "W07", "W08", "W09", "Broome", "WOL"}
Dim dest() As String = New String() {"ADE", "BRI", "MEL", "CAN", "CEN", "DAR", "GDC", "HOB", "LAU", "N01", "N02", "N03", "N04", "N05", "N06", "NCL", "PER", "Q01", "Q02", "Q03", "Q04", "Q05", "Q06", "Q07", "Q08", "S02", "S03", "S04", "Kang Is", "SUN", "SYD", "T02", "TSV", "V01", "V02", "V03", "W01", "W02", "W03", "W04", "W05", "W06", "W07", "W08", "W09", "Broome", "WOL"}
Dim sht() As String = New String() {"KG Dead Weight Cost", "KG Dead + 20%", "Kg Dead + 30%", "Kg Dead + 40%", "Kg Cost 150 cube", "Kg 150 + 20%", "Kg 150 + 30%", "Kg 150 + 40%", "Kg Cost 180 cube", "Kg 180 + 20%", "Kg 180 + 30%", "Kg 180 + 40%", "Kg Cost 200 cube", "Kg 200 + 20%", "Kg 200 + 30%", "Kg 200 + 40%", "Kg Cost 250 cube", "Kg 250 + 20%", "Kg 250 + 30%", "Kg 250 + 40%"}
Dim a As Integer
Dim b As Integer
Dim c As Integer
For a = 0 To UBound(ex)
Me.DropDownList1.Items.Add(New ListItem(ex(a)))
Next
For b = 0 To UBound(dest)
Me.DropDownList2.Items.Add(New ListItem(dest(b)))
Next
For c = 0 To UBound(sht)
Me.DropDownList3.Items.Add(New ListItem(sht(c)))
Next
End If

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim Range As String
Range = DropDownList3.SelectedItem.Text

' Create connection string variable. Modify the "Data Source" parameter as
' appropriate for your environment.
Dim sConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Data Source=" & Server.MapPath("../CostMatrix.xls") _
& ";" & "Extended Properties=Excel 8.0;"

' Create the connection object by using the preceding connection string.
Dim objConn As New OleDbConnection(sConnectionString)

' Open connection with the database.
objConn.Open()

' Create new OleDbCommand to return data from worksheet.
Dim objCmdSelect1 As New OleDbCommand("SELECT [" & DropDownList2.SelectedItem.Text & "]" & " FROM [Basic Charges$A14:co61] WHERE F1 = '" & DropDownList1.SelectedItem.Text & "'", objConn)

' Create new OleDbDataAdapter that is used to build a DataSet
' based on the preceding SQL SELECT statement.
Dim objAdapter1 As New OleDbDataAdapter

' Pass the Select command to the adapter.
objAdapter1.SelectCommand = objCmdSelect1

' Create new DataSet to hold information from the worksheet.
Dim objDataset1 As New DataSet
Dim objDataVal As String

' Fill the DataSet with the information from the worksheet.
objAdapter1.Fill(objDataset1, "XLData")

' Build a table from the original data.
DataGrid1.DataSource = objDataset1.Tables(0).DefaultView
DataGrid1.DataBind()

Dim objCmdSelect2 As New OleDbCommand("SELECT [" & DropDownList2.SelectedItem.Text & "]" & " FROM [" & Range & "$A3:AV5] WHERE Ex = '" & DropDownList1.SelectedItem.Text & "'", objConn)

' Create new OleDbDataAdapter that is used to build a DataSet
' based on the preceding SQL SELECT statement.
Dim objAdapter2 As New OleDbDataAdapter

' Pass the Select command to the adapter.
objAdapter2.SelectCommand = objCmdSelect2

' Create new DataSet to hold information from the worksheet.
Dim objDataset2 As New DataSet

' Fill the DataSet with the information from the worksheet.
objAdapter2.Fill(objDataset2, "XLData")

' Build a table from the original data.
DataGrid2.DataSource = objDataset2.Tables(0).DefaultView
DataGrid2.DataBind()

' Clean up objects.
objConn.Close()

Dim Basic As Decimal
Dim Additional As Decimal

Basic = DataGrid1.Items(0).Cells(0).Text
Additional = DataGrid2.Items(0).Cells(0).Text
LabelText.Text = (Additional + Basic)
LabelText2.Text = (Additional + Basic + 0.03)

End Sub
End Class

Mike Rosenblum
08-02-2004, 10:31 AM
There's a lot of code there, but I think the error message itself is basically telling you the story:"Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index" When you hit the error, .Net will also tell you the full Stack/Path of the errors from source of the Error to the top level caller.

When you hit this error, the first issue is on what line is it occurring? The 2nd issue is what is the value of the 'Index' parameter that is being passed in. If it is indexing a 1-Based collection, this 'Index' value must be a number between 1 and Collection.Count. If it's a 0-Based collection, then 'Index' must be between 0 and Collection.Count -1.

So, running it within the IDE, you need to see (1) on which line is the error, (2) what is the value of the 'Index' parameter and (3) what is the Collection's total Count at that point.

Let us know what you find...

kookaburra
08-02-2004, 05:33 PM
Hi Mike,

Thanks for the help. I hope this is the information you asked for...

It crashes on line 133:
Line 132: Basic = DataGrid1.Items(0).Cells(0).Text
Line 133: Additional = DataGrid2.Items(0).Cells(0).Text
Line 134: LabelText.Text = (Additional + Basic)
Line 135: LabelText2.Text = (Additional + Basic + 0.03)

Here is the Stack Trace:

[ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index]
System.Collections.ArrayList.get_Item(Int32 index) +91
System.Web.UI.WebControls.DataGridItemCollection.get_Item(Int32 index) +10
CostMatrix.WebForm1.Button1_Click(Object sender, EventArgs e) in C:\Inetpub\wwwroot\net\CostMatrix\WebForm1.aspx.vb:133
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.R aisePostBackEvent(String eventArgument) +57
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain() +1277

I have also noticed that if I comment out lines 132 - 135, it does not crash, but it only returns a value for DataGrid2 if Ex (DropDownList1) is one of the first 2 selections. DataGrid1 seems to work fine.

Mike Rosenblum
08-02-2004, 05:53 PM
I'm sorry that I don't know the DataGrid control at all... But kicking it around for the 1st time just now, it seems that you can either Call DataGrid.Items() in one of two variations:

(1) DataGrid.Items(RowIndex as Integer, ColIndex as Integer)
(2) DataGrid.Items(Cell as System.Windows.Form.DataGridCell)

In short, if you wish to index the DataGrid with an Integer, you must pass it two Integers, to give it the coordinates.

-- Mike

kookaburra
08-02-2004, 09:42 PM
I found what was wrong and Í was so stupid I could cry.

[" & Range & "$A3:AV5] should have been [" & Range & "$A3:AV50]

It was not taking the entire range of the spreadsheet into account.

Thanks for helping :-)

Mike Rosenblum
08-03-2004, 07:34 AM
Ugh, sorry, that stinks... :(

Glad you sorted it out, although, I don't see how ' DataGrid2.Items(0)' can work... Was I wrong about it requiring two indexes to reference the .Items() Property?

kookaburra
08-03-2004, 07:11 PM
I'm sorry Mike - I am so new to .NET that I don't know how to answer your question. I am sure you are correct, but my guessing won't help anyone. I am just so amazed and happy that my code works.

:-)

Mike Rosenblum
08-03-2004, 07:16 PM
Heh, no worries... I'm glad you're up and running too! :)

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum