[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Loop through Dataset to remove records not matching key

Posted on 2006-05-02
2
Medium Priority
?
405 Views
Last Modified: 2008-02-20
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
0
Comment
Question by:peterdidow
2 Comments
 
LVL 34

Accepted Solution

by:
Sancler earned 2000 total points
ID: 16589335
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
 
LVL 11

Expert Comment

by:MacNuttin
ID: 16589336
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Screencast - Getting to Know the Pipeline
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question