locked
System.IndexOutOfRangeException: There is no row at position 5 RRS feed

  • Question

  • User-2054142495 posted

    System.IndexOutOfRangeException: There is no row at position 5.

    I have the folowing codeblock in my application. This is to enable me to remove rows from a gridview without deleting the same rows completley from the database!

    The problem is I am able to remove values but not all of the values when i select in a particular order or more than 3!

    This is the error I'm met with!

    System.IndexOutOfRangeException: There is no row at position 5.

    The comented catch exeption solves the problem slightly.

    Dim strQuery As String = "SELECT UserId, staffNo, forename, surname FROM userProfilesTbl WHERE st_id=@st_id AND wa_id=@wa_id ORDER BY surname ASC "
            Dim cmd As New SqlCommand(strQuery)
            cmd.Parameters.AddWithValue("@st_id", myLblSt.Value().ToString)
            cmd.Parameters.AddWithValue("@wa_id", myLblWa.Value().ToString)
    
            Dim dt As DataTable = GetDataConn(cmd)
            'Try
            For Each dr In GridView1.Rows
    
                Dim chkBox As CheckBox = dr.FindControl("RowLevelCheckBox")
    
                If chkBox.Checked = True Then
    
                    dt.Rows.RemoveAt(dr.RowIndex)
                    dt.AcceptChanges()
    
                End If
            Next
            'Catch ex As Exception
    
            'End Try
            GridView1.DataSource = dt
            GridView1.DataBind()



    Friday, January 25, 2013 8:29 AM

Answers

  • User-2054142495 posted

     Solved the problem and heres the code. Thanks

    Dim i As Integer = (GridView1.Rows.Count - 1)
            Do While (i >= 0)
                If CType(GridView1.Rows(i).FindControl("RowLevelCheckBox"), CheckBox).Checked = True Then
                   
                    dt.Rows(i).Delete()
                    dt.AcceptChanges()
    
                End If
                i = (i - 1)
            Loop



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 12, 2013 12:05 PM

All replies

  • User-1716253493 posted
    maybe you need to count how many rows you have remove. Try : dt.Rows.RemoveAt (dr.RowIndex - removedrows)
    Friday, January 25, 2013 8:40 AM
  • User-2054142495 posted

    cant see how that syntax will help me! What does removerows reference?

    Friday, January 25, 2013 8:45 AM
  • User-2054142495 posted

    By calling the rows.Delete() method followed by AcceptChanges() method I've removed the initial error however i will remove rows 1 to 6 in that order! If lets say I try to remove rows 3 to 6 then then it does'nt remove the rows selected but the first 3??

    dt.Rows(0).Delete()
    dt.AcceptChanges()

    Any ideas?

    Tuesday, January 29, 2013 6:01 PM
  • User-650628323 posted

    Hi,

    If lets say I try to remove rows 3 to 6 then then it does'nt remove the rows selected but the first 3??

    For your issue, you can try to set the index which you want to remove.

    Best wishes,

    Tuesday, January 29, 2013 8:33 PM
  • User1052662409 posted

    count rows

    like (C#)

    if (dt.rows.count>0)

    {

    //do somthing

    }

    I guess there not enough rows

    Wednesday, January 30, 2013 4:17 AM
  • User-2054142495 posted

    Trying to do my count  with a while loop but still not working!

    in BOLD the first error 'find control is not a member of system array'

    and the second 'Value of type 'String' cannot be converted to '1-dimensional array of String'

    Any Ideas?

     Dim dt As DataTable = GetDataConn(cmd)
         
            Dim dr() As DataRow
            Dim rowsToRemove As List(Of DataRow) = New List(Of DataRow)
            Dim a As Integer = 0
            Do While (a < dr.Length)
                rowsToRemove.Add(dr(a))
                a = (a + 1)
            Loop
            
    
            For Each dr In GridView1.Rows
    
                Dim chkBox As CheckBox = dr.FindControl("RowLevelCheckBox")
    
                If chkBox.Checked = True Then
    
                    dt.Rows.RemoveAt(dr)
                    dt.AcceptChanges()
                End If
    
            Next
    
            GridView1.DataSource = dt
            GridView1.DataBind()
    
        End Sub
    


    Wednesday, January 30, 2013 6:30 AM
  • User-2054142495 posted

     Solved the problem and heres the code. Thanks

    Dim i As Integer = (GridView1.Rows.Count - 1)
            Do While (i >= 0)
                If CType(GridView1.Rows(i).FindControl("RowLevelCheckBox"), CheckBox).Checked = True Then
                   
                    dt.Rows(i).Delete()
                    dt.AcceptChanges()
    
                End If
                i = (i - 1)
            Loop



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 12, 2013 12:05 PM