Advertisement

06.02.2006 at 03:44AM PDT, ID: 21872278
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.6

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

Asked by LoveToSpod in Programming for ASP.NET

Tags: , , , ,

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


Start Free Trial
[+][-]06.02.2006 at 03:51AM PDT, ID: 16815475

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]06.02.2006 at 04:10AM PDT, ID: 16815554

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.02.2006 at 04:18AM PDT, ID: 16815578

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.02.2006 at 04:18AM PDT, ID: 16815579

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.02.2006 at 06:03AM PDT, ID: 16816208

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: Programming for ASP.NET
Tags: out, must, less, than, size
Sign Up Now!
Solution Provided By: vinodhsomasekharan
Participating Experts: 3
Solution Grade: A
 
 
[+][-]06.02.2006 at 07:00AM PDT, ID: 16816700

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.02.2006 at 07:30AM PDT, ID: 16816988

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
 
Loading Advertisement...
20081112-EE-VQP-42