trying to cache a dataSet

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I insert into the cache:
if (Cache["testQuestion"+ i] == null)
{
Cache.Insert("testQuestion"+ i, ds.Tables);
}
 
Maybe the Cache["testQuestion"+ i] is not null?

try

if (Cache["testQuestion"+ i] == null)
{
Cache.Insert("testQuestion"+ i, ds.Tables);
}
else
Response.Write("CACHE IS NOT NULL");
 
I checked, it is not null.

I have added more code below, on how i insert and then retrieve frm the
cache. The error is not in the inserting but in the retrieving frm the cache.

I insert into the cache:
Eg. i = 1.

if (Cache["testQuestion"+ i] == null)
{
Cache.Insert("testQuestion"+ i, ds.Tables)
}
else
lbl2.Text += "CACHE IS NOT NULL";

retrieve from the cache:

DataSet ds2 = (DataSet)Cache["testQuestion"+ (int)ViewState["QnNo"]]; <------
if (ds2 != null)
{
ds2 = (DataSet)Cache["testQuestion"+ (int)ViewState["QnNo"]];
MyRepeater.DataSource = ds.Tables[(int)ViewState["QnNo"]];
MyRepeater.DataBind();
}

I get the error: "Specified cast is not valid". TIA.
 
yes, of course

you casting it to a dataset when it is infact a datatable

so either
Cache.Insert("testQuestion"+ i, ds)

or better yet
DataTable dt2 = (DataTable)Cache["testQuestion"+
(int)ViewState["QnNo"]];

because ds.Tables is infact a DataTable in a DataSet
 
Back
Top