Cache problem
-
I have four select statement in 1 stored procedure, I will use it for 4 different dropdownlist.
SqlDataAdapter sqlda = new SqlDataAdapter("populatelist_sp", osql);
sqlda.Fill(DS);Cache.Add("listTables", DS, null, DateTime.Now.AddMinutes(60), TimeSpan.Zero, System.Web.Caching.CacheItemPriority.Normal, null);
DropDownList1.DataSource = DS;
DropDownList1.DataBind();The PROBLEM: When I used the cache object to populate another dropdownlist it gives me error "Object reference" meaning the value in the cache object is NULL. Out of 10 run I encounter 8 times.
if (DropDownList2.Items.Count == 0)
{
DataSet localDS = new DataSet();
localDS = (DataSet)Cache["listTables"];DataView dv = new DataView(localDS.Tables\[1\]); _**\--- Failed on this part**_ DropDownList2.DataSource = dv; DropDownList2.DataBind();
}
Is there any settings that I need to do to retain the value inside the cache object? GAIN: Instead of 4 roundtrip to the database in getting the value of the dropdownlist I only used 1 connection and the rest is Caching dependent. Regards :confused:
Dabsukol
-
I have four select statement in 1 stored procedure, I will use it for 4 different dropdownlist.
SqlDataAdapter sqlda = new SqlDataAdapter("populatelist_sp", osql);
sqlda.Fill(DS);Cache.Add("listTables", DS, null, DateTime.Now.AddMinutes(60), TimeSpan.Zero, System.Web.Caching.CacheItemPriority.Normal, null);
DropDownList1.DataSource = DS;
DropDownList1.DataBind();The PROBLEM: When I used the cache object to populate another dropdownlist it gives me error "Object reference" meaning the value in the cache object is NULL. Out of 10 run I encounter 8 times.
if (DropDownList2.Items.Count == 0)
{
DataSet localDS = new DataSet();
localDS = (DataSet)Cache["listTables"];DataView dv = new DataView(localDS.Tables\[1\]); _**\--- Failed on this part**_ DropDownList2.DataSource = dv; DropDownList2.DataBind();
}
Is there any settings that I need to do to retain the value inside the cache object? GAIN: Instead of 4 roundtrip to the database in getting the value of the dropdownlist I only used 1 connection and the rest is Caching dependent. Regards :confused:
Dabsukol
dabuskol wrote:
localDS = (DataSet)Cache["listTables"];
You have not checked if the cache is still valid.
dabuskol wrote:
DataView dv = new DataView(localDS.Tables[1]); --- Failed on this part
Are you sure the dataset have at least 2 tables, on every call?
xacc.ide - now with TabsToSpaces support
IronScheme - 1.0 beta 1 - out now!
((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x)) -
dabuskol wrote:
localDS = (DataSet)Cache["listTables"];
You have not checked if the cache is still valid.
dabuskol wrote:
DataView dv = new DataView(localDS.Tables[1]); --- Failed on this part
Are you sure the dataset have at least 2 tables, on every call?
xacc.ide - now with TabsToSpaces support
IronScheme - 1.0 beta 1 - out now!
((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))Definitely yes, before assigning it to cache the Dataset (DS) has four tables, after assigning to cache object I can still see the 4 tables. But only after pressing the 2nd dropdownlist with postback where data should come from cache object instead of database the value of cache is null.
Dabsukol
-
Definitely yes, before assigning it to cache the Dataset (DS) has four tables, after assigning to cache object I can still see the 4 tables. But only after pressing the 2nd dropdownlist with postback where data should come from cache object instead of database the value of cache is null.
Dabsukol
-
That is quite interesting. You not running out of memory for caching perhaps?
xacc.ide - now with TabsToSpaces support
IronScheme - 1.0 beta 1 - out now!
((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))Nop. I just change the CACHE to ViewState Object and it work. Though I believe Viewstate is not the solution since I have 10 dropdownlist in every page. Viewstate will make my page heavy becuase of the encrypted data. I tried to search the Net and I can't find any solution to this problem
Dabsukol
-
I have four select statement in 1 stored procedure, I will use it for 4 different dropdownlist.
SqlDataAdapter sqlda = new SqlDataAdapter("populatelist_sp", osql);
sqlda.Fill(DS);Cache.Add("listTables", DS, null, DateTime.Now.AddMinutes(60), TimeSpan.Zero, System.Web.Caching.CacheItemPriority.Normal, null);
DropDownList1.DataSource = DS;
DropDownList1.DataBind();The PROBLEM: When I used the cache object to populate another dropdownlist it gives me error "Object reference" meaning the value in the cache object is NULL. Out of 10 run I encounter 8 times.
if (DropDownList2.Items.Count == 0)
{
DataSet localDS = new DataSet();
localDS = (DataSet)Cache["listTables"];DataView dv = new DataView(localDS.Tables\[1\]); _**\--- Failed on this part**_ DropDownList2.DataSource = dv; DropDownList2.DataBind();
}
Is there any settings that I need to do to retain the value inside the cache object? GAIN: Instead of 4 roundtrip to the database in getting the value of the dropdownlist I only used 1 connection and the rest is Caching dependent. Regards :confused:
Dabsukol
It's normal for contents to get removed from the cache. When you get the value from the cache you have to check if you get a null reference back. If the cache grows too much, it will naturally remove large objects first. Perhaps your objects will stay longer in the cache if you store the tables as four separate cache objects.
Despite everything, the person most likely to be fooling you next is yourself.
-
It's normal for contents to get removed from the cache. When you get the value from the cache you have to check if you get a null reference back. If the cache grows too much, it will naturally remove large objects first. Perhaps your objects will stay longer in the cache if you store the tables as four separate cache objects.
Despite everything, the person most likely to be fooling you next is yourself.
Thanks for all the help. After so many trials and isolation, the only problem I believe that is missing why the data on the CACHE object is missing si the declaration of the namespace of CACHE. using System.Web.Caching --- After including it in my code. it works perfect.
Dabsukol