Wednesday, March 28, 2012

Strange cache behavior

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,

-StanWith 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" <nospam@.yahoo.com> wrote in message
news:O8yxgvZmEHA.3900@.TK2MSFTNGP10.phx.gbl...
> 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
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" <kspencer@.takempis.com> wrote in message
news:OYhkV$ZmEHA.3336@.TK2MSFTNGP10.phx.gbl...
> 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" <nospam@.yahoo.com> wrote in message
> news:O8yxgvZmEHA.3900@.TK2MSFTNGP10.phx.gbl...
>> 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 wrote:

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

--

SevDer
http://www.sevder.com
> You need to update cache with the latest version. It doesn't
> automatically update cache.

That is what I thought, but it does update itslef by reference..
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
Hi Stan,

Have you had any further ideas on this issue? If there is anything else we
can help, please feel free to post here. Thanks.

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

0 comments:

Post a Comment