Strange cache behavior

  • Thread starter Thread starter Stan
  • Start date Start date
S

Stan

Not sure if this is by design or not, but it looks weird.

I get a datatable from cache:

dt = (DataTable) Cache[CacheKey];

Then, depending on selection in dropdown list I filter it:

DataView dv = dt.DefaultView;
if (cboStatus.SelectedIndex != 0)
{
int Status = int.Parse (cboStatus.SelectedItem.Value);
dv.RowFilter = string.Format ("CollectionStatusKey = {0}", Status);
}

And then, bind it to the datagrid:

grdMain.DataSource = dv;
grdMain.DataBind();

However, if dataview filter is set first time, it stays this way. What I
mean is the change in dataview gets stored in cache and dt = (DataTable)
Cache[CacheKey] brings back the old value on the next postback.

The question is this: if I add an item to cache and then modify this item,
would the modified item be automatically changed in cache? In other words,
is cache set by value or by reference?

Thanks,

-Stan
 
With reference types, by reference. A DataTable is a reference type.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
I get paid good money to
solve puzzles for a living
 
if you dont want that behavior then always store a copy.
set a copy and get a copy

--

Regards,

Hermit Dave
(http://hdave.blogspot.com)
Kevin Spencer said:
With reference types, by reference. A DataTable is a reference type.

--
HTH,
Kevin Spencer
.Net Developer
Microsoft MVP
I get paid good money to
solve puzzles for a living

Stan said:
Not sure if this is by design or not, but it looks weird.

I get a datatable from cache:

dt = (DataTable) Cache[CacheKey];

Then, depending on selection in dropdown list I filter it:

DataView dv = dt.DefaultView;
if (cboStatus.SelectedIndex != 0)
{
int Status = int.Parse (cboStatus.SelectedItem.Value);
dv.RowFilter = string.Format ("CollectionStatusKey = {0}", Status);
}

And then, bind it to the datagrid:

grdMain.DataSource = dv;
grdMain.DataBind();

However, if dataview filter is set first time, it stays this way. What I
mean is the change in dataview gets stored in cache and dt = (DataTable)
Cache[CacheKey] brings back the old value on the next postback.

The question is this: if I add an item to cache and then modify this
item,
would the modified item be automatically changed in cache? In other
words,
is cache set by value or by reference?

Thanks,

-Stan
 
Stan said:
Not sure if this is by design or not, but it looks weird.

I get a datatable from cache:

dt = (DataTable) Cache[CacheKey];

Then, depending on selection in dropdown list I filter it:

DataView dv = dt.DefaultView;
if (cboStatus.SelectedIndex != 0)
{
int Status = int.Parse (cboStatus.SelectedItem.Value);
dv.RowFilter = string.Format ("CollectionStatusKey = {0}", Status);
}

And then, bind it to the datagrid:

grdMain.DataSource = dv;
grdMain.DataBind();

However, if dataview filter is set first time, it stays this way. What I
mean is the change in dataview gets stored in cache and dt = (DataTable)
Cache[CacheKey] brings back the old value on the next postback.

The question is this: if I add an item to cache and then modify this item,
would the modified item be automatically changed in cache? In other words,
is cache set by value or by reference?

Thanks,

-Stan
You need to update cache with the latest version. It doesn't
automatically update cache.
 
Yes Stan,

The Cache collection maintain the reference type objects by reference is by
design behavior and regarding on this, we can just think it as other
serverside data stores such as SessionState , ApplicationState....

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
Back
Top