• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 26439
  • Last Modified:

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


0
LoveToSpod
Asked:
LoveToSpod
3 Solutions
 
SandeepRRCommented:
You can try with putting breakPoint on that statement and open the
Immediate Windows
and type e.Item.ItemIndex in that and hit the enter
u will get the current value for the INdex and see whether it is less than 0 or grtaer than the total no of rows in the grid

How to open Immediate Windows
Menu Debud>Windows>Immediate

by this atleast u can know the why the error is thr

SandeepRR
0
 
LoveToSpodAuthor Commented:
Cheers!

While I'm doing that can you see anything wrong witht the code, or is this a common thing with a simple fix?
0
 
LoveToSpodAuthor Commented:
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?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LoveToSpodAuthor Commented:
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?
0
 
vinodhsomasekharanCommented:

Hai ,

try assiging the "DataKeyField" property in the "BindGrid(ByVal strSort As String)" method.

as like in the paging method.,
        DataGrid1.DataSource = DataSet1
        DataGrid1.DataKeyField = "ID"
        DataGrid1.DataBind()

Regards,
--Vinu.
0
 
deightonCommented:
>>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
0
 
vinodhsomasekharanCommented:
Hai,

You r using DataGrid1.DataKeys(e.Item.ItemIndex)

DataKeys() collection is populated only if u assign the DataKeyfield property while binding the grid

ie DataGrid1.DataKeyField = "ID" .. before binding the grid..

but in u r code.. u r binding the grid in the page_load event..u r using the funtion

Sub BindGrid(ByVal strSort As String)
        SqlDataAdapter1.Fill(DataSet1)
        DataView1 = DataSet1.Tables(0).DefaultView
        DataView1.Sort = strSort
        DataGrid1.DataSource = DataSet1
        DataGrid1.DataBind()
End Sub

but u have not set the DataKeyField value here..
so try the code DataGrid1.DataKeyField = "ID"  in the bindgrid funtion before DataBind()

Sub BindGrid(ByVal strSort As String)
        SqlDataAdapter1.Fill(DataSet1)
        DataView1 = DataSet1.Tables(0).DefaultView
        DataView1.Sort = strSort
        DataGrid1.DataSource = DataSet1
       DataGrid1.DataKeyField = "ID"
        DataGrid1.DataBind()
End Sub

Also just ensure at the point of deletecommand.. what's the value in the  DataGrid1.DataKeys() collection

I think u have only one primary key/ ID for that row.. so try using
DataGrid1.DataKeys(0)   in the delete command instead of  DataGrid1.DataKeys(e.Item.ItemIndex)  ..

hope this helps u..

regards
Vinodh




0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now