Loop through Dataset to remove records not matching key

I have a dataset and a hashtable. I need to loops through my dataset and remove any values that are not in my hashtable "CompletedList" and then rebing my datagrid to my dataset.

This is the code for the sub routine (which doesnt work - returns an error of:
There is no row at position 1582.
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.IndexOutOfRangeException: There is no row at position 1582.

Source Error:


Line 622:                If CompletedList.ContainsKey(key) Then
Line 623:                    validkey = key
Line 624:                    If Convert.ToInt32(dt.Rows(i).Item(0)) <> Convert.ToInt32(CompletedList.Item(validkey)) Then
Line 625:                        dt.Rows.RemoveAt(i)
Line 626:                    End If
 

Source File: c:\inetpub\wwwroot\bookstore\ssl\Management\Controls\NewFeaturedAdmin.ascx.vb    Line: 624

Stack Trace:


[IndexOutOfRangeException: There is no row at position 1582.]
   System.Data.DataRowCollection.get_Item(Int32 index)
   StoreFront.StoreFront.NewFeaturedAdmin.MaintainList() in c:\inetpub\wwwroot\bookstore\ssl\Management\Controls\NewFeaturedAdmin.ascx.vb:624
   StoreFront.StoreFront.NewFeaturedAdmin.Page_PreRender(Object sender, EventArgs e) in c:\inetpub\wwwroot\bookstore\ssl\Management\Controls\NewFeaturedAdmin.ascx.vb:553
   System.Web.UI.Control.OnPreRender(EventArgs e)
   System.Web.UI.Control.PreRenderRecursiveInternal()
   System.Web.UI.Control.PreRenderRecursiveInternal()
   System.Web.UI.Control.PreRenderRecursiveInternal()
   System.Web.UI.Page.ProcessRequestMain()

 


--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:1.1.4322.2032; ASP.NET Version:1.1.4322.2032


-----------------------------------------------------------------------------------
The Code:


Public Sub MaintainList()
        ' This sub will go through the current datagrid page
        ' and add checked items to the hashtables
        ' as well as remove unchecked items if they are currently
        ' in the hashtables.

        Dim da As New SqlClient.SqlDataAdapter

        Dim dt As New DataTable
        Dim ds As DataSet
        For Each dgItem As DataGridItem In dgProducts22.Items
            Dim key As String = dgItem.Cells.Item(1).Text
            Dim Name As String = dgItem.Cells.Item(2).Text
            Dim path As String = dgItem.Cells.Item(7).Text
            Dim UID As String = dgItem.Cells.Item(0).Text



            If CompletedList.ContainsKey(key) Then
                CheckBox = CType(dgItem.FindControl("chkSelected"), CheckBox)
                If CheckBox.Checked = False Then
                    CheckBox.Checked = True
                End If
            End If


            If Not CompletedList.ContainsKey(key) Then
                CheckBox = CType(dgItem.FindControl("chkSelected"), CheckBox)
                CheckBox.Checked = False
            End If

            If CheckBox.Checked = True And Not CompletedList.ContainsKey(key) Then
                'add the item to the hashtable
                CompletedList.Add(key, UID)

            End If

            If CheckBox.Checked = False And CompletedList.ContainsKey(key) Then
                'Remove the item to the hashtable
                CompletedList.Remove(key)

            End If



            If CheckBox.Checked = True And Not CompletedList_Name.ContainsKey(key) Then
                CompletedList_Name.Add(key, Name)
            Else
                CompletedList_Name.Remove(key)
            End If

            If CheckBox.Checked And Not CompletedList_ImagePath.ContainsKey(key) Then
                CompletedList_ImagePath.Add(key, path)
            Else
                CompletedList_ImagePath.Remove(key)
            End If
            'Dataset itself needs to be update with the values in our completedList ( and remove those that are not)
            'The problem right now is when we change the page we rebind to the datset which contains our original values
           
            ds = Session("DS")
            dt = ds.Tables(0)
           
            Dim i As Integer
           
            For i = 0 To dt.Rows.Count - 1
                Dim validkey As String
                If CompletedList.ContainsKey(key) Then
                    validkey = key
                    If Convert.ToInt32(dt.Rows(i).Item(0)) <> Convert.ToInt32(CompletedList.Item(validkey)) Then
                        dt.Rows.RemoveAt(i)
                    End If
                End If


            Next
         


        Next
        ds.Tables(0).BeginLoadData()
        da.Fill(ds, dt.TableName)
        ds.Tables(0).EndLoadData()
        Session("DS") = ds
        ds = Nothing
    End Sub
peterdidowAsked:
Who is Participating?
 
SanclerConnect With a Mentor Commented:
Change this

            For i = 0 To dt.Rows.Count - 1
                Dim validkey As String
                If CompletedList.ContainsKey(key) Then
                    validkey = key
                    If Convert.ToInt32(dt.Rows(i).Item(0)) <> Convert.ToInt32(CompletedList.Item(validkey)) Then
                        dt.Rows.RemoveAt(i)
                    End If
                End If


to this

            For i = dt.Rows.Count - 1 To 0 Step -1 '<<< AMENDED LINE
                Dim validkey As String
                If CompletedList.ContainsKey(key) Then
                    validkey = key
                    If Convert.ToInt32(dt.Rows(i).Item(0)) <> Convert.ToInt32(CompletedList.Item(validkey)) Then
                        dt.Rows.RemoveAt(i)
                    End If
                End If

The problem here is that, as you remove an earlier row, the numbers of all subsequent rows reduce by 1.  If you tackle it in reverse order, the numbers of (earlier) rows that you still want to look at remain unchanged.

Roger
0
 
MacNuttinCommented:
There is either 1581 rows in your dataset and the loop is not ending properly or there is a null value at row 1582 that you're not accomodating
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.