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.IndexOutOfRangeExce
ption: 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(CompletedL
ist.Item(v
alidkey)) Then
Line 625: dt.Rows.RemoveAt(i)
Line 626: End If
Source File: c:\inetpub\wwwroot\booksto
re\ssl\Man
agement\Co
ntrols\New
FeaturedAd
min.ascx.v
b Line: 624
Stack Trace:
[IndexOutOfRangeException:
There is no row at position 1582.]
System.Data.DataRowCollect
ion.get_It
em(Int32 index)
StoreFront.StoreFront.NewF
eaturedAdm
in.Maintai
nList() in c:\inetpub\wwwroot\booksto
re\ssl\Man
agement\Co
ntrols\New
FeaturedAd
min.ascx.v
b:624
StoreFront.StoreFront.NewF
eaturedAdm
in.Page_Pr
eRender(Ob
ject sender, EventArgs e) in c:\inetpub\wwwroot\booksto
re\ssl\Man
agement\Co
ntrols\New
FeaturedAd
min.ascx.v
b:553
System.Web.UI.Control.OnPr
eRender(Ev
entArgs e)
System.Web.UI.Control.PreR
enderRecur
siveIntern
al()
System.Web.UI.Control.PreR
enderRecur
siveIntern
al()
System.Web.UI.Control.PreR
enderRecur
siveIntern
al()
System.Web.UI.Page.Process
RequestMai
n()
--------------------------
----------
----------
----------
----------
----------
----
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("
chkSelecte
d"), CheckBox)
If CheckBox.Checked = False Then
CheckBox.Checked = True
End If
End If
If Not CompletedList.ContainsKey(
key) Then
CheckBox = CType(dgItem.FindControl("
chkSelecte
d"), 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.Contain
sKey(key) Then
CompletedList_Name.Add(key
, Name)
Else
CompletedList_Name.Remove(
key)
End If
If CheckBox.Checked And Not CompletedList_ImagePath.Co
ntainsKey(
key) Then
CompletedList_ImagePath.Ad
d(key, path)
Else
CompletedList_ImagePath.Re
move(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(CompletedL
ist.Item(v
alidkey)) 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