Forum Moderators: open

Message Too Old, No Replies

Strange DataView Problem

         

pedrodepacos

9:25 am on Aug 30, 2003 (gmt 0)

10+ Year Member



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...

 
'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...

(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?

korkus2000

5:49 pm on Sep 5, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't know VB.net, but I do work with C#.

Do you need to call disposal methods in VB.net or is it just a formality to let the garbage collector know what it can kill?

In C# using a disposal method does not gaurantee a time when the garbage collection will happen. It just lets the collector know it is available. Could your problem be that it is collecting the memory too quickly?

pedrodepacos

7:41 pm on Sep 5, 2003 (gmt 0)

10+ Year Member



I think the garbage disposal is similar between VB and C#. I think that you may be onto something, but my question would be, why doesn't this occur when I use a dataset to bind to the datagrid? I call dispose for the dataset in exactly the same manner as I do for the dataview.