-
Strange DataView Problem
Hello,
I need some clarification about a stange DataView behaviour I have noticed. I am developing a dynamic website that is binding data (from an Access db) to a datagrid. I use a cached dataset to save trips to the db, here is the code...
Code:
'populate the Cache Variable that stores the Web Content Dataset
Public Function PopulateWebContentDataset(Optional ByVal blnRepopulate As Boolean = False) As Boolean
Dim ds As New DataSet()
'check if cached dataset already exists
If Current.Cache("WebContent") Is Nothing = False Then
'the cached variable already exists
Return True
End If
'get the Web Content from the database
Dim cn As New OleDbConnection(AppSettings("cnStringWebContent"))
Dim da As New OleDbDataAdapter("SELECT * FROM tblWebContent", cn)
Try
'fill the Web Content dataset with records from the database
da.Fill(ds, "tblWebContent")
If ds.Tables("tblWebContent").Rows.Count > 0 Then
'add the dataset to the cache
Current.Cache.Insert("WebContent", ds, _
New System.Web.Caching.CacheDependency(AppSettings("dbPathWebContent")), _
DateTime.Now.AddDays(1), Current.Cache.NoSlidingExpiration, _
System.Web.Caching.CacheItemPriority.Normal, Nothing)
Return True
Else
'no records were found
Current.Cache.Remove("WebContent")
Current.Cache("WebContent") = Nothing
Return False
End If
Catch DefaultExc As Exception
Finally
cn.Dispose()
da.Dispose()
ds.Dispose()
End Try
End Function
Public Function BindPageContent(ByRef dgr As DataGrid, ByVal strPage As String, _
Optional ByVal blnRepopulate As Boolean = False) As Boolean
'check if the cache Web Content dataset already exists
If PopulateWebContentDataset(blnRepopulate) = False And blnRepopulate = False Then
'unable to get the information from the database
Return False
End If
Dim ds As DataSet
Dim dv As New DataView()
Try
ds = Current.Cache("WebContent")
'ds.Tables("tblWebContent").DefaultView.RowFilter = "Page=" & "'" & strPage & "'"
dv = ds.Tables("tblWebContent").DefaultView
dv.RowFilter = "Page=" & "'" & strPage & "'"
dgr.DataSource = dv
dgr.DataBind()
Catch DefaultExc As Exception
Finally
ds.Dispose()
dv.Dispose()
End Try
End Function
My question is, the above code works perfectly the first time the page loads but when I return to this specific page (which should be getting the cached dataset), nothing is displayed. I checked the dataset and it has the correct amount of rows, however, the dataview has a rowcount of 0 (when it previously had a rowcount of 1).
I have done the following to fix this problem, all of which have worked...
1. Instead of getting the dataset from the cache, I connected to the db on each page_load and repopulated the dataset
2. When I bind the datagrid using the dataset as the datasource instead of the dataview, it works...
Code:
(ds.tables("tblWebContent").defaultview.rowfilter="Page='" & strPage & "'")
3. When I take out dv.dispose, everything works as expected
I have many instances in my code where I use the exact same code as posted above and it doesn't seem like there is a problem with the DataView. Should I re-examine all my code and remove dataview.dispose()? Does anyone have any suggestions on why this may be happening?