Strange cache behavior

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
 
K

Kevin Spencer

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
 
H

Hermit Dave

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
 
S

SevDer

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

Steven Cheng[MSFT]

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
 

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

Forum statistics

Threads
473,996
Messages
2,570,237
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top