[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[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!

9.8

Loop through Dataset to remove records not matching key

Asked by peterdidow in Microsoft Visual Basic.Net

Tags: dataset, loop, through

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
[+][-]05/02/06 11:45 AM, ID: 16589335Accepted Solution

View this solution now by starting your 30-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: Microsoft Visual Basic.Net
Tags: dataset, loop, through
Sign Up Now!
Solution Provided By: Sancler
Participating Experts: 2
Solution Grade: A
 
[+][-]05/02/06 11:45 AM, ID: 16589336Expert Comment

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 30-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091111-EE-VQP-92