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. General Programming
  3. C#
  4. Cache problem

Cache problem

Scheduled Pinned Locked Moved C#
databasehelpjsonquestion
7 Posts 3 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.
  • D Offline
    D Offline
    dabuskol
    wrote on last edited by
    #1

    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

    L G 2 Replies Last reply
    0
    • D dabuskol

      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

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #2

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

      D 1 Reply Last reply
      0
      • L leppie

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

        D Offline
        D Offline
        dabuskol
        wrote on last edited by
        #3

        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

        L 1 Reply Last reply
        0
        • D dabuskol

          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

          L Offline
          L Offline
          leppie
          wrote on last edited by
          #4

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

          D 1 Reply Last reply
          0
          • L leppie

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

            D Offline
            D Offline
            dabuskol
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • D dabuskol

              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

              G Offline
              G Offline
              Guffa
              wrote on last edited by
              #6

              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.

              D 1 Reply Last reply
              0
              • G Guffa

                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.

                D Offline
                D Offline
                dabuskol
                wrote on last edited by
                #7

                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

                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