Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Problems with caching datatable

Problems with caching datatable

Scheduled Pinned Locked Moved ASP.NET
helpperformancequestion
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    livez
    wrote on last edited by
    #1

    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!

    T 1 Reply Last reply
    0
    • L livez

      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!

      T Offline
      T Offline
      ToddHileHoffer
      wrote on last edited by
      #2

      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

      L 1 Reply Last reply
      0
      • T ToddHileHoffer

        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

        L Offline
        L Offline
        livez
        wrote on last edited by
        #3

        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

        T 2 Replies Last reply
        0
        • L livez

          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

          T Offline
          T Offline
          ToddHileHoffer
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • L livez

            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

            T Offline
            T Offline
            ToddHileHoffer
            wrote on last edited by
            #5

            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

            L 1 Reply Last reply
            0
            • T ToddHileHoffer

              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

              L Offline
              L Offline
              livez
              wrote on last edited by
              #6

              Thank you very much for your responses, I shall try your suggestions ( and check out that article ) first thing I do when I arrive at work tomorrow. /Regards

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups