Caching a dataset

C

Craig G

im having my first play about with caching a dataset but seem to be having some problems in doing so!!

basically i have a dataset which i add to the cache as follows

Cache.Insert("myDS", dsResults, Nothing, DateTime.Now.AddMinutes(5), TimeSpan.Zero)



i then have a select button which i click so that the page is reloaded. so that i can only get the dataset from the cache

but when i do the following
dsResults = New DataSet

dsResults = Cache.Item("myDS")



i get Referenced object has a value of 'Nothing'.

what am i doing wrong??
Cheers,
Craig
 
S

Scott M.

To store the dataset:

Cache.Insert("myDS", dsResults)

To retrieve the dataset:

dim ds as DataSet 'Note the omission of the "New" keyword
ds = Cache.Item("myDS")


im having my first play about with caching a dataset but seem to be having some problems in doing so!!

basically i have a dataset which i add to the cache as follows

Cache.Insert("myDS", dsResults, Nothing, DateTime.Now.AddMinutes(5), TimeSpan.Zero)



i then have a select button which i click so that the page is reloaded. so that i can only get the dataset from the cache

but when i do the following
dsResults = New DataSet

dsResults = Cache.Item("myDS")



i get Referenced object has a value of 'Nothing'.

what am i doing wrong??
Cheers,
Craig
 
G

Guest

Cache.Insert("myDS", dsResults, Nothing,
System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds(300))

Craig, the last argument is how long to stay in cache, the one above I keep
it there for 5 minutes. Try that instead of Zero

Al
 
C

Craig G

nope still no joy , used both your ideas

but as soon as i click the button myDS is lost!!

ive checked before the button click and it does create it and there is data
in it
here is my button click code

Private Sub btnSearch_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnSearch.Click

Dim strCode As String = txtCode.Text

Dim strDescription As String = txtDescription.Text

Dim strFilter As String

Dim drFilteredRows As DataRow()

Dim dtDataTable As New DataTable

Dim dsResults As DataSet



dsResults = CType(Cache.Item("myDS"), DataSet)

strFilter = "Code='" & strCode & "' AND Description='" & strDescription &
"'"

drFilteredRows = dsResults.Tables(0).Select(strFilter)
 
S

Scott M.

Craig, the code I gave you is the correct code (note that you don't create
a new dataset when retrieving), but you say it doesn't work when you hit the
Back button. Well, hitting the Back button does not cause the page code to
be executed, it only brings up what the browser has stored in its LOCAL
cache from the last time this page was called. Hit the REFRESH button to
cause the page code to be run.
 
V

vMike

im having my first play about with caching a dataset but seem to be having
some problems in doing so!!

basically i have a dataset which i add to the cache as follows

Cache.Insert("myDS", dsResults, Nothing, DateTime.Now.AddMinutes(5),
TimeSpan.Zero)

i then have a select button which i click so that the page is reloaded. so
that i can only get the dataset from the cache

but when i do the following
dsResults = New DataSet
dsResults = Cache.Item("myDS")

i get Referenced object has a value of 'Nothing'.

what am i doing wrong??
Cheers,
Craig


This function is a bit more generic but it may help you.

Public Shared Function mgGetMetaTagsXML() as datatable
dim dt as datatable
'HttpContext.Current.cache.remove("MetaData") 'for testing
dt = CType(HttpContext.Current.Cache("MetaData"), DataTable)
if dt is nothing then
Dim ds AS DataSet = New Dataset
Dim strPathFull as string = strPath &
ConfigurationSettings.AppSettings("dbroot") & "tblMetaTags.xml"
ds.ReadXml(strPathFull, XmlReadMode.InferSchema)
dt =ds.tables(0)
ds = nothing
HttpContext.Current.Cache.Insert ("MetaData",dt,New
CacheDependency(strPathFull),DateTime.MaxValue, TimeSpan.FromMinutes(60))

end if
return dt
End Function

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top