Problems with caching datatable
-
Hello! I cache a System.Data.Datatable (which I create with lazyloading) containing urlrewriting information. I then use a method looking like this:
string GetDataFromTable(MyEnum enum)
The method creates a system.data.dataview pointing at the table with the enum as rowfilter and returns a few values from the view. My problem is, sometimes when the method is run it generates an error like this: "The given key was not present in the dictionary." since my enum only contains a few values and I know for a fact that all enum-values are represented in the table, this error should not be able to occur. As I see it the problem must therefore be either with the caching or something with the memory. Has anyone else experienced this problem or have an idea how I approach this? /Regards!
-
Hello! I cache a System.Data.Datatable (which I create with lazyloading) containing urlrewriting information. I then use a method looking like this:
string GetDataFromTable(MyEnum enum)
The method creates a system.data.dataview pointing at the table with the enum as rowfilter and returns a few values from the view. My problem is, sometimes when the method is run it generates an error like this: "The given key was not present in the dictionary." since my enum only contains a few values and I know for a fact that all enum-values are represented in the table, this error should not be able to occur. As I see it the problem must therefore be either with the caching or something with the memory. Has anyone else experienced this problem or have an idea how I approach this? /Regards!
How are you caching the table? Can you post the line of code (and surrounding lines) that is causing the problem?
I didn't get any requirements for the signature
-
How are you caching the table? Can you post the line of code (and surrounding lines) that is causing the problem?
I didn't get any requirements for the signature
My cached datatable:
private static DataTable MyTable
{
get
{
object obj = HttpContext.Current.Cache[SiteCacheEntries.MyTable];
if (obj == null)
{
DataTable dt = Data.Specific.GetMyTable();DateTime nextMorning = DateTime.Now.Hour < 6 ? new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 6, 0, 0) : new DateTime(DateTime.Now.AddDays(1).Year, DateTime.Now.AddDays(1).Month, DateTime.Now.AddDays(1).Day, 6, 0, 0); HttpContext.Current.Cache.Insert(SiteCacheEntries.MyTable, dt, null, nextMorning, Cache.NoSlidingExpiration); return dt; } return (DataTable)obj; }
}
Dataview is also cached:
public static DataView MyDataView
{
get
{
object obj = HttpContext.Current.Cache[SiteCacheEntries.MyDataView];
if (obj == null)
{
DataView dv = new DataView(MyTable);HttpContext.Current.Cache.Insert(SiteCacheEntries.MyDataView, dv, null, DateTime.Now.AddYears(10), Cache.NoSlidingExpiration); return dv; } return (DataView)obj; } }
My method to collect data from table
public static string GetPageConstant(PageConstants type, LanguageCode lang)
{
MyDataView.RowFilter = string.Format("{0} = '{1}' AND {2} = '{3}'", _nameOfTypeColumn,
typeColumn.ToString(), _nameOfLanguageColumn, lang);if (MyDataView.Count > 0) { return MyDataView\[0\].Row\[\_nameOfValueToCollectColumn\].ToString(); } return string.Empty; }
And its in the GetPageConstant method the error occurs.
modified on Monday, August 3, 2009 10:06 AM
-
My cached datatable:
private static DataTable MyTable
{
get
{
object obj = HttpContext.Current.Cache[SiteCacheEntries.MyTable];
if (obj == null)
{
DataTable dt = Data.Specific.GetMyTable();DateTime nextMorning = DateTime.Now.Hour < 6 ? new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 6, 0, 0) : new DateTime(DateTime.Now.AddDays(1).Year, DateTime.Now.AddDays(1).Month, DateTime.Now.AddDays(1).Day, 6, 0, 0); HttpContext.Current.Cache.Insert(SiteCacheEntries.MyTable, dt, null, nextMorning, Cache.NoSlidingExpiration); return dt; } return (DataTable)obj; }
}
Dataview is also cached:
public static DataView MyDataView
{
get
{
object obj = HttpContext.Current.Cache[SiteCacheEntries.MyDataView];
if (obj == null)
{
DataView dv = new DataView(MyTable);HttpContext.Current.Cache.Insert(SiteCacheEntries.MyDataView, dv, null, DateTime.Now.AddYears(10), Cache.NoSlidingExpiration); return dv; } return (DataView)obj; } }
My method to collect data from table
public static string GetPageConstant(PageConstants type, LanguageCode lang)
{
MyDataView.RowFilter = string.Format("{0} = '{1}' AND {2} = '{3}'", _nameOfTypeColumn,
typeColumn.ToString(), _nameOfLanguageColumn, lang);if (MyDataView.Count > 0) { return MyDataView\[0\].Row\[\_nameOfValueToCollectColumn\].ToString(); } return string.Empty; }
And its in the GetPageConstant method the error occurs.
modified on Monday, August 3, 2009 10:06 AM
I don't think you should ever cache a DataView. You should re-create it each time you need it. public static string GetPageConstant(PageConstants type, LanguageCode lang) { DataView MyDataView = new DataView(MyTable); MyDataView.RowFilter = string.Format("{0} = '{1}' AND {2} = '{3}'", _nameOfTypeColumn, typeColumn.ToString(), _nameOfLanguageColumn, lang); MyDataView.ToTable().Rows[_nameOfValueToCollectColumn].ToString(); return string.Empty; } also you could try MyTable.Select(string.Format("{0} = '{1}' AND {2} = '{3}'", _nameOfTypeColumn, typeColumn.ToString(), _nameOfLanguageColumn, lang))
I didn't get any requirements for the signature
-
My cached datatable:
private static DataTable MyTable
{
get
{
object obj = HttpContext.Current.Cache[SiteCacheEntries.MyTable];
if (obj == null)
{
DataTable dt = Data.Specific.GetMyTable();DateTime nextMorning = DateTime.Now.Hour < 6 ? new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 6, 0, 0) : new DateTime(DateTime.Now.AddDays(1).Year, DateTime.Now.AddDays(1).Month, DateTime.Now.AddDays(1).Day, 6, 0, 0); HttpContext.Current.Cache.Insert(SiteCacheEntries.MyTable, dt, null, nextMorning, Cache.NoSlidingExpiration); return dt; } return (DataTable)obj; }
}
Dataview is also cached:
public static DataView MyDataView
{
get
{
object obj = HttpContext.Current.Cache[SiteCacheEntries.MyDataView];
if (obj == null)
{
DataView dv = new DataView(MyTable);HttpContext.Current.Cache.Insert(SiteCacheEntries.MyDataView, dv, null, DateTime.Now.AddYears(10), Cache.NoSlidingExpiration); return dv; } return (DataView)obj; } }
My method to collect data from table
public static string GetPageConstant(PageConstants type, LanguageCode lang)
{
MyDataView.RowFilter = string.Format("{0} = '{1}' AND {2} = '{3}'", _nameOfTypeColumn,
typeColumn.ToString(), _nameOfLanguageColumn, lang);if (MyDataView.Count > 0) { return MyDataView\[0\].Row\[\_nameOfValueToCollectColumn\].ToString(); } return string.Empty; }
And its in the GetPageConstant method the error occurs.
modified on Monday, August 3, 2009 10:06 AM
BTW, I wrote an article about this. An Easy Way to Cache Static Data in ASP.NET[^]
I didn't get any requirements for the signature
-
BTW, I wrote an article about this. An Easy Way to Cache Static Data in ASP.NET[^]
I didn't get any requirements for the signature