ObjectDataSource and CacheKeyDependency problem RRS feed

  • Question

  • User-1164060753 posted

    I have noticed a strange behavior with ObjectDataSource using CacheKeyDependency

    If I set the CacheKeyDependency, the ObjectDataSource.Selecting event (and selected) gets called twice!!! (and of course the Database Select Method)

    If I remove the CacheKeyDependency the ODS Selecting occurs only once!

    Speed concerning apart, this could potentially break my code-behind logic if I assume that Selecting/Selected gets called only once...

    Have any of you noticed that?

    This behavior could be credited to ASP.NET or there some bug in my code, maybe javascript or something different?

    PS: The timeline of the events are like so, with in bold the duplicated ones:

    DataLayer Select

    DataLayer Select


    Tuesday, October 23, 2012 10:40 AM

All replies

  • User3866881 posted


    The ObjectDataSource control automatically caches data when the EnableCaching property is set to true and theCacheDuration property is set to a value greater than 0, which indicates the number of seconds that the cache stores data before the cache entry is discarded. A value of 0 indicates an infinitely long cache.

    So you can remove this or just set a period of time so as to let it "overdue" after a period of time.


    Wednesday, October 24, 2012 8:32 PM
  • User-1164060753 posted

    I know that, but it's not what I am asking!

    My post points out that the Selecting event is called TWICE

    That seems like a bug to me, or probably I am overlooking something else... but what?

    Thursday, October 25, 2012 4:09 AM
  • User3866881 posted


    Hello again,

    This seems strange, can you offer us a detailled sample for us to analyze?

    Thursday, October 25, 2012 4:16 AM
  • User-1164060753 posted

    The original code uses an .ascx Control, which itself uses a TxtBox for User input and filtering, the GridView is inside an UpdatePanel and jQuery is used for triggering the UpdatePanel refresh upon User input, ecc... so it could be quite disguising...

    So I have started from scratch, only using my BLL class to Get Data.

    1 gridView with Paging, one ODS with/without CacheKeyDependency. 

    I haven't yet reproduced the twice Select calling behavior, however already with these simple code there's something different which
    happens enabling CacheKeyDependency.

    Indeed, this could then be related to my problem (... or not?)

    here is the code:


    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CacheKeyDependencyTwice.aspx.cs"
        Inherits="WebSpace.Web.PagineProva.CacheKeyDependencyTwice" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <form id="form1" runat="server">
            <asp:GridView ID="GridView1" runat="server" AllowPaging="True" DataSourceID="ODSData"
            <asp:ObjectDataSource ID="ODSData" runat="server" EnableCaching="True" OldValuesParameterFormatString="original_{0}"
                OnSelected="ODSData_Selected" OnSelecting="ODSData_Selecting" SelectMethod="GetRicambiWithDisponibilitaByDesc_WithoutParameters"


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Diagnostics;
    namespace WebSpace.Web.PagineProva
        public partial class CacheKeyDependencyTwice : System.Web.UI.Page
            protected void Page_Load(object sender, EventArgs e)
                Debug.WriteLine(DateTime.Now.ToLongTimeString() +  ": Page_Load");
            protected void ODSData_Selected(object sender, ObjectDataSourceStatusEventArgs e)
                Debug.WriteLine(DateTime.Now.ToLongTimeString() + ": ODSData_Selected");
            protected void ODSData_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
                Debug.WriteLine(DateTime.Now.ToLongTimeString() + ": ODSData_Selecting");

    I Debug (F5) it, and change a couple of pages on the GridView.


    'iisexpress.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\e1842388\72ad48b8\App_Theme_grids.rcldnk8g.dll', Symbols loaded.
    14:39:23: Page_Load
    14:39:23: ODSData_Selecting
    14:39:24: ODSData_Selected
    14:39:26: Page_Load
    14:39:28: Page_Load

    Then I enable 



    14:41:48: Page_Load
    14:41:48: ODSData_Selecting
    14:41:49: ODSData_Selected
    14:41:51: Page_Load
    14:41:51: ODSData_Selecting
    14:41:51: ODSData_Selected
    14:41:54: Page_Load
    14:41:54: ODSData_Selecting
    14:41:54: ODSData_Selected

    Thursday, October 25, 2012 8:42 AM
  • User-1164060753 posted


    In fact there is no problem ... at least not the one I thought to have spotted

    It was my mistake, re-checking all the code I found that, due to one "terrible" hack of mine to retrieve the total count of filtered records, I make a call to set GridView.PageIndex = 
    So with this call, together with the right initial call, I end up having TWO SELECT CALLS...

    So, the only difference is just and only that above, namely that in the first case, without CacheKeyDependency, the Select is not re-invoked during the filtering of the data.

    Thursday, October 25, 2012 11:03 AM
  • User3866881 posted

    Hello TheRed again;)

    It seems that you've solved your problem?

    if yes, I'll close the issue or you can continue to feedback;)

    Thursday, October 25, 2012 9:12 PM
  • User-1164060753 posted

    Hello again!

    Indeed, I'd like to have your feedback on the question posted on my post above (link http://forums.asp.net/post/5191402.aspx)

    In short terms the question is: 

    If I enable CacheKeyDependency, each time I switch a page on the paged GridView, I get another Selected event followed by Filtering event, while if I remove the CacheKeyDependency I only get the Filtering event !?

    Friday, October 26, 2012 3:54 AM
  • User13489231 posted

    The Cache value for the CacheKeyDependency must be set. If not, the ObjectDataSource cache is essentially disabled, which can result in the select method being called twice.

    See the example in the Microsoft documentation.

        protected void Page_Load(object sender, EventArgs e)
            if (!IsPostBack)
                Cache[ObjectDataSource2.CacheKeyDependency] = "CacheExample";


    Tuesday, April 2, 2013 5:18 PM