Link to home
Start Free TrialLog in
Avatar of LoveToSpod
LoveToSpodFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Index was out of range. Must be non-negative and less than the size of the collection. Parameter name index

Hi there,

I am using the delete command within ASP DataGrid. When I click the delete link on the datagrid, I get the following error:

<<Index was out of range. Must be non-negative and less than the size of the collection. Parameter name index>>

The error appears on line 411. I have included both the unhandled exception detail, and the complete application script.

Can you help me dubug, I'm lost trying to fix this one.

Cheers - LoveToSpod

 --------------------------------------------------------------------
UNHANDLED EXEPTION DETAIL - START
 --------------------------------------------------------------------
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

Source Error:

Line 409:    Private Sub DataGrid1_DeleteCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.DeleteCommand
Line 410:
Line 411:        SqlCommand2.Parameters("@ID").Value = DataGrid1.DataKeys(e.Item.ItemIndex)
Line 412:        SqlConnection1.Open()
Line 413:        SqlCommand2.ExecuteNonQuery()
 

Source File: c:\inetpub\wwwroot\TechnicalCourier\EditTechCourier.aspx.vb    Line: 411

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.DataKeyCollection.get_Item(Int32 index) +10
   TechnicalCourier.EditTechCourier.DataGrid1_DeleteCommand(Object source, DataGridCommandEventArgs e) in c:\inetpub\wwwroot\TechnicalCourier\EditTechCourier.aspx.vb:411
   System.Web.UI.WebControls.DataGrid.OnDeleteCommand(DataGridCommandEventArgs e)
   System.Web.UI.WebControls.DataGrid.OnBubbleEvent(Object source, EventArgs e) +589
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +26
   System.Web.UI.WebControls.DataGridItem.OnBubbleEvent(Object source, EventArgs e) +106
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +26
   System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +121
   System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +115
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +138
   System.Web.UI.Page.ProcessRequestMain() +1277

 --------------------------------------------------------------------
UNHANDLED EXEPTION DETAIL - END
 --------------------------------------------------------------------


 --------------------------------------------------------------------
APPLICATION SCRIPT - START
 --------------------------------------------------------------------


Public Class EditTechCourier
    Inherits System.Web.UI.Page
    Public SortValue As String
    Protected WithEvents SqlCommand2 As System.Data.SqlClient.SqlCommand
    Public StrCurrentPageIndex As String


    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here

        If Not Page.IsPostBack Then
            BindGrid("[BusinessUnit]")

        End If
    End Sub
    Sub BindGrid(ByVal strSort As String)

        SqlDataAdapter1.Fill(DataSet1)
        DataView1 = DataSet1.Tables(0).DefaultView
        DataView1.Sort = strSort
        DataGrid1.DataSource = DataSet1
        DataGrid1.DataBind()

    End Sub


    Private Sub DataGrid1_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.EditCommand
        DataGrid1.EditItemIndex = e.Item.ItemIndex
        BindGrid(SortValue)

    End Sub

    Private Sub DataGrid1_CancelCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.CancelCommand
        DataGrid1.EditItemIndex = -1
        BindGrid(SortValue)

    End Sub

    Private Sub DataGrid1_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.UpdateCommand
        'Retrieve textbox controls from datagrid


        'Dim txtBusinessUnit As TextBox
        'Dim txtCountry As TextBox
        Dim txtMinDelCost As TextBox
        Dim txtDelCostLoadKM As TextBox
        Dim txtDelSchedCost As TextBox
        Dim txtSetDelTime As TextBox
        Dim txtSetDelWindow As TextBox
        Dim txtWeekendSvc As TextBox
        Dim txtUnpack As TextBox
        Dim txtPack As TextBox
        Dim txtSvcConnect As TextBox
        Dim txtSvcDisconnect As TextBox
        Dim txtSWAPUnpackPack As TextBox
        Dim txtSWAPDisconRecon As TextBox
        Dim txtSvcProvider As TextBox
        Dim txtSvcMode As TextBox
        Dim txtAddTimeCost As TextBox
        Dim txtComments As TextBox


        'txtBusinessUnit = CType(e.Item.Cells(2).Controls(0), TextBox)
        'txtCountry = CType(e.Item.Cells(3).Controls(0), TextBox)
        txtMinDelCost = CType(e.Item.Cells(4).Controls(0), TextBox)
        txtDelCostLoadKM = CType(e.Item.Cells(5).Controls(0), TextBox)
        txtDelSchedCost = CType(e.Item.Cells(6).Controls(0), TextBox)
        txtSetDelTime = CType(e.Item.Cells(7).Controls(0), TextBox)
        txtSetDelWindow = CType(e.Item.Cells(8).Controls(0), TextBox)
        txtWeekendSvc = CType(e.Item.Cells(9).Controls(0), TextBox)
        txtUnpack = CType(e.Item.Cells(10).Controls(0), TextBox)
        txtPack = CType(e.Item.Cells(11).Controls(0), TextBox)
        txtSvcConnect = CType(e.Item.Cells(12).Controls(0), TextBox)
        txtSvcDisconnect = CType(e.Item.Cells(13).Controls(0), TextBox)
        txtSWAPUnpackPack = CType(e.Item.Cells(14).Controls(0), TextBox)
        txtSWAPDisconRecon = CType(e.Item.Cells(15).Controls(0), TextBox)
        txtSvcProvider = CType(e.Item.Cells(16).Controls(0), TextBox)
        txtSvcMode = CType(e.Item.Cells(17).Controls(0), TextBox)
        txtAddTimeCost = CType(e.Item.Cells(18).Controls(0), TextBox)
        txtComments = CType(e.Item.Cells(19).Controls(0), TextBox)

        'Assign params to sqlcommand
        'SqlCommand1.Parameters("@BusinessUnit").Value = txtBusinessUnit.Text
        'SqlCommand1.Parameters("@Country").Value = txtCountry.Text
        SqlCommand1.Parameters("@MinDelCost").Value = txtMinDelCost.Text
        SqlCommand1.Parameters("@DelCostLoadKM").Value = txtDelCostLoadKM.Text
        SqlCommand1.Parameters("@DelSchedCost").Value = txtDelSchedCost.Text
        SqlCommand1.Parameters("@SetDelTime").Value = txtSetDelWindow.Text
        SqlCommand1.Parameters("@SetDelWindow").Value = txtSetDelWindow.Text
        SqlCommand1.Parameters("@WeekendSvc").Value = txtWeekendSvc.Text
        SqlCommand1.Parameters("@Unpack").Value = txtUnpack.Text
        SqlCommand1.Parameters("@Pack").Value = txtPack.Text
        SqlCommand1.Parameters("@SvcConnect").Value = txtSvcConnect.Text
        SqlCommand1.Parameters("@SvcDisconnect").Value = txtSvcDisconnect.Text
        SqlCommand1.Parameters("@SWAPUnpackPack").Value = txtSWAPUnpackPack.Text
        SqlCommand1.Parameters("@SWAPDisconRecon").Value = txtSWAPDisconRecon.Text
        SqlCommand1.Parameters("@SvcProvider").Value = txtSvcProvider.Text
        SqlCommand1.Parameters("@SvcMode").Value = txtSvcMode.Text
        SqlCommand1.Parameters("@AddTimeCost").Value = txtAddTimeCost.Text
        SqlCommand1.Parameters("@Comments").Value = txtComments.Text
        SqlCommand1.Parameters("@ID").Value = DataGrid1.DataKeys(e.Item.ItemIndex)

        'execute sqlcommand

        SqlConnection1.Open()
        SqlCommand1.ExecuteNonQuery()

        'Deselct row for editing
        DataGrid1.EditItemIndex = -1
        BindGrid(SortValue)
        SqlConnection1.Close()

    End Sub




    Private Sub DataGrid1_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles DataGrid1.PageIndexChanged
        DataGrid1.CurrentPageIndex = e.NewPageIndex
        StrCurrentPageIndex = e.NewPageIndex
        DataGrid1.EditItemIndex = -1
        SqlDataAdapter1.Fill(DataSet1)
        DataView1 = DataSet1.Tables(0).DefaultView
        DataView1.Sort = SortValue
        DataGrid1.DataSource = DataSet1
        DataGrid1.DataKeyField = "ID"
        DataGrid1.DataBind()





    End Sub


    Sub DataGrid1_SortCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles DataGrid1.SortCommand
        SortValue = e.SortExpression
        BindGrid(SortValue)
    End Sub


    Private Sub DataGrid1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGrid1.SelectedIndexChanged
        BindGrid(SortValue)
    End Sub

    Private Sub DataGrid1_DeleteCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.DeleteCommand

        SqlCommand2.Parameters("@ID").Value = DataGrid1.DataKeys(e.Item.ItemIndex)
        SqlConnection1.Open()
        SqlCommand2.ExecuteNonQuery()
        BindGrid(SortValue)
        SqlConnection1.Close()

    End Sub
End Class


 --------------------------------------------------------------------
APPLICATION SCRIPT - END
 --------------------------------------------------------------------


SOLUTION
Avatar of SandeepRR
SandeepRR
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of LoveToSpod

ASKER

Cheers!

While I'm doing that can you see anything wrong witht the code, or is this a common thing with a simple fix?
I did the breakpoint and hovered over (e.Item.ItemIndex)  - the number returned (as should be is '3'). This is not less than 0 or greater than the total no of rows in the grid. Now I've ascertained that, where do I look now?
I did the breakpoint and hovered over (e.Item.ItemIndex)  - the number returned (as should be is '3'). This is not less than 0 or greater than the total no of rows in the grid. Now I've ascertained that, where do I look now?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>This is not less than 0 or greater than the total no of rows in the grid.

if there's 3 rows then it's a maximum of 2?  Or am I being dumb here
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial