Results 1 to 8 of 8

Thread: need help with SqlCacheDependency

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    need help with SqlCacheDependency

    I have this code:

    Code:
            protected void Button3_Click(object sender, EventArgs e)
            {
                DataView dv2 = (System.Data.DataView)Cache["SqlSource"];
                if (dv2 == null)
                {
                 
                    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["SampleDBConnectionString"].ConnectionString);
                    SqlCommand cmd = new SqlCommand("Select id, product, warehouse, quantity from inventory", con);
                    SqlCacheDependency sqlDep = new SqlCacheDependency(cmd);
    
                    SqlDataSource1.SelectCommand = "Select id, product, warehouse, quantity from inventory";
                    dv2 = (System.Data.DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
    
                    Cache.Insert("SqlSource", dv2, sqlDep, System.Web.Caching.Cache.NoAbsoluteExpiration,new TimeSpan(0, 0, 0, 5));
                }
                int x = dv2.Table.Rows.Count;
                Label2.Text = x.ToString();
            }
    For some reason, the Cache is not expiring. What is wrong with this code? I am aware that I could do this using the SqlDataSource1.SqlCacheDependency property, but I'd like to know how to do it using the SqlCacheDependency object. Could someone tell me why this code is not working?
    Last edited by benmartin101; Sep 12th, 2011 at 03:14 AM.

  2. #2
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: need help with SqlCacheDependency

    You are using the NoAbsoluteExpiration, means

    Public Shared ReadOnly NoAbsoluteExpiration As Date
    Member of System.Web.Caching.Cache
    Summary:
    Used in the absoluteExpiration parameter in an System.Web.Caching.Cache.Insert(System.String,System.Object) method call to indicate the item should never expire. This field is read-only.
    Use this one

    Code:
                    Cache.Insert("SqlSource", dv2, sqlDep,,new TimeSpan(0, 0, 0, 5),System.Web.Caching.Cache.NoSlidingExpiration);
    Please mark you thread resolved using the Thread Tools as shown

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    Re: need help with SqlCacheDependency

    Thanks. I'll give it a try, and let you know.

  4. #4
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: need help with SqlCacheDependency

    Hello Ben,

    Did this work out for you, or are you still having issues?

    Thanks

    Gary

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    Re: need help with SqlCacheDependency

    Quote Originally Posted by gep13 View Post
    Hello Ben,

    Did this work out for you, or are you still having issues?

    Thanks

    Gary
    Hey gep, sorry for the late response. Well, good news is that it is now expiring. I had to change it to this:
    Code:
    Cache.Insert("SqlSource", dv2, sqlDep, System.DateTime.UtcNow.Add(new TimeSpan(0,0,60)), System.Web.Caching.Cache.NoSlidingExpiration); //the 4th parameter had to be a DateTime type
    So the above code basically causes the cache to expire. Bad news is, it is always expiring regardless of whether or not I make changes to the table.

    Basically what I am trying to accomplish (using the methods above) is that when there are no changes to the result based on the select command, it should not expire. If there are changes, then it should expire on the given datetime/timespan.

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    Re: need help with SqlCacheDependency

    Actually, shouldn't it also become invalidated (or null) when there changes made to the table (affecting the select statement)?

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    Re: need help with SqlCacheDependency

    When I change:

    Code:
    SqlCommand cmd = new SqlCommand("Select id, product, warehouse, quantity from inventory", con);
                    SqlCacheDependency sqlDep = new SqlCacheDependency(cmd);
    to
    Code:
    SqlCacheDependency sqlDep = new SqlCacheDependency("db1", "inventory") //db1 is from the webconfig
    
    <caching>
          <sqlCacheDependency enabled="true">
            <databases>
              <add name="db1" connectionStringName="SampleDBConnectionString" pollTime="10000"/>
            </databases>
          </sqlCacheDependency>
        </caching> ;
    It works fine. The cache["SqlSource"] become invalidated/null. But how come it does not work with the original code (the one with the Select statement)?

  8. #8
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: need help with SqlCacheDependency

    Hello,

    In all honesty, I am not sure

    Sorry!

    Gary

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width