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

  • Question

  • User-1024202203 posted

    I am Getting this exception when i select the checkbox control in Grid view"Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index"

    I have Checkbox in Gridview control, when i select the record in grid view corresponding entire record will be populated into form fields,it working absolutely fine,I have search method on my webform, ifi searched based on particular value in form fileds, records will be filtered in Girdvie control when i select the record values are not loading above error occuring.normally no issues when i search and select error occuring. 

     Protected Sub chkSelect_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
            Dim indx As Integer
            Dim connectionString As String = "Database=pjt;" & "Data Source=localhost;" & "User Id=root;Password=prasad123"
            Dim query As String = "Select WONumber,PartNumber,Rev,customer,CRep,AEng,Orderqty,PReceived,FabDue,FabComp,AssyDue,AssyComp,TestDue,TestComp,Notes,Shippedqty,status from status"
            'Dim query As String = "Select partnumber from status"
            Dim connection As New MySqlConnection(connectionString)
            Dim da As New MySqlDataAdapter(query, connection)
            Dim ds As New DataSet()
            Dim myAdapter As New MySqlDataAdapter
            myAdapter = New MySqlDataAdapter(query, connection)
            myAdapter.Fill(ds, "status")
            Dim dt As DataTable = New DataTable() 'DataTable Initializes a new instance of the DataTable class with no arguments. 
            Dim checkbox As CheckBox = CType(sender, CheckBox)
            Dim row As GridViewRow = CType(checkbox.NamingContainer, GridViewRow)
            Dim i As Integer
            For i = 0 To dt.Rows.Count - 1 Step i + 1
                If (dt.Rows(i)("WONumber").ToString = row.Cells(1).Text.ToString()) Then
                    indx = i
                    Dim loopCheckBox As CheckBox = CType(GVStatus.Rows(i).Cells(0).Controls(1), CheckBox)
                    loopCheckBox.Checked = False
                End If
            If (checkbox.Checked) Then
                txtWON.Text = dt.Rows(indx)("WONumber").ToString()
                txtPartNumber.Text = dt.Rows(indx)("PartNumber").ToString()
                txtRev.Text = dt.Rows(indx)("Rev").ToString()
                txtCust.Text = dt.Rows(indx)("Customer").ToString()
                DDCR.Text = dt.Rows(indx)("CRep").ToString()
                DDAE.Text = dt.Rows(indx)("AEng").ToString()
                txtOQty.Text = dt.Rows(indx)("OrderQty").ToString()
                DDPR.Text = dt.Rows(indx)("PReceived").ToString
                txtFDD.Text = dt.Rows(indx)("FabDue").ToString()
                txtFCD.Text = dt.Rows(indx)("FabComp").ToString()
                txtADD.Text = dt.Rows(indx)("AssyDue").ToString
                txtACD.Text = dt.Rows(indx)("AssyComp").ToString()
                txtTDD.Text = dt.Rows(indx)("TestDue").ToString()
                txtTCD.Text = dt.Rows(indx)("TestComp").ToString()
                txtDes.Text = dt.Rows(indx)("Notes").ToString()
                txtShipQty.Text = dt.Rows(indx)("ShippedQty").ToString()
                DDStatus.Text = dt.Rows(indx)("Status").ToString()
            End If
        End Sub

     Error Occured at this line

    Dim loopCheckBox As CheckBox = CType(GVStatus.Rows(i).Cells(0).Controls(1), CheckBox)

     Please let me know if you need aditional information, thanks


    Thursday, February 24, 2011 4:35 AM

All replies

  • User551462331 posted

    try changing this line as

    Dim loopCheckBox As CheckBox = CType(GVStatus.Rows(row.RowIndex).Cells(0).Controls(1), CheckBox)

    hope this helps....

    Thursday, February 24, 2011 4:50 AM
  • User-1024202203 posted

    Thanks, when i changed no values are populated into Form fileds

    Thursday, February 24, 2011 4:58 AM