Geeks With Blogs
AzamSharp Some day I will know everything. I hope that day never comes.

AJAX is on my mind for quite some time now. I was implementing a functionality just like google suggest using AJAX.NET library when I ran into an interesting problem.

Basically I have a method named FillList this method is marked with [Ajax.Method] attribute and hence has the power of being called from the client side. FillList method in turn calls GetDataSet method which returns the DataSet. Here is the implementation of the GetDataSet method.

private DataSet GetDataSet() 
        {
            DataSet ds = 
null;             
            
                            
                
string query = @"SELECT * FROM tblPerson";
                SqlConnection myConnection = 
new SqlConnection(GetConnectionString()); 
                SqlDataAdapter ad = 
new SqlDataAdapter(query,myConnection); 
                ds = 
new DataSet(); 
                ad.Fill(ds,"tblPerson");  
                ViewState["MyDataSet"] = ds; 
            
            
return ds; 
        }

I know the implementation of the method is bad (performance wise) as I am accessing the database on every call. This means that every key the user presses the method GetDataSet will be called. So, I tried to implement the Caching on the DataSet.

I change the method to this:

private DataSet GetDataSet() 
        {
            DataSet ds = 
null;             
            
                    
            
if(Cache["MyDataSet"] != null
            {
                ds = (DataSet) Cache["MyDataSet"]; 
            }
            
else 
            

                
string query = @"SELECT * FROM tblPerson";
                SqlConnection myConnection = 
new SqlConnection(GetConnectionString()); 
                SqlDataAdapter ad = 
new SqlDataAdapter(query,myConnection); 
                ds = 
new DataSet(); 
                ad.Fill(ds,"tblPerson");  
                
// DataSet is valid for 10 minutes and after that it expires
                
Cache.Insert("MyDataSet",ds,null,DateTime.Now.AddMinutes(10),TimeSpan.Zero); 
            }
            
            
return ds; 
        }

Now according to me this should fix the problem of accessing the database again and again but this introduces another juicy nugget. If you try the above code (The one that uses Caching) you will see that the code always returns "null". There is no error generated on the server side as well as the client side.

The Reason:

The reason is that AJAX is not able to access the Cache object since its created and maintained on the server. AJAX is used to make the server side calls NOT to access the variables created on the server.

You can however cache the AJAX method:

[Ajax.AjaxMethod(30)] // This will be cached for 30 seconds
  


 

powered by IMHO

 

Posted on Saturday, October 1, 2005 10:18 PM | Back to top


Comments on this post: Remote Scripting Caching Problems (AJAX)

# re: Remote Scripting Caching Problems (AJAX)
Requesting Gravatar...
You can access the cache object using System.Web.HttpContext.Current.Cache!

CIAO
Michael
Left by Michael Schwarz on Oct 06, 2005 10:20 AM

# re: Remote Scripting Caching Problems (AJAX)
Requesting Gravatar...
Thanks Michael. I will try it out and let you know.

Thanks,
Left by Azam on Oct 06, 2005 10:24 AM

# re: Remote Scripting Caching Problems (AJAX)
Requesting Gravatar...
Thanks Michael it worked!



Left by Azamsharp on Oct 13, 2005 7:49 PM

# re: Remote Scripting Caching Problems (AJAX)
Requesting Gravatar...
Hi,

I am also facing the same kind of problem with AJAX.

Cpuld you please post the code which you use to solve your problem using System.Web.HttpContext.Current.Cache

Cheers
Ken
Left by Ken on Jan 11, 2006 7:27 AM

# re: Remote Scripting Caching Problems (AJAX)
Requesting Gravatar...
//-cache on Ajax method--------
if (System.Web.HttpContext.Current.Cache[messagetext] != null)
{
oTextListHolder = (TextListHolder) System.Web.HttpContext.Current.Cache[messagetext];
}
else
{
oTextListHolder = new TextListHolder();
oParams[0] = qaReasonType;
oTextListHolder.Load("DMSGGR",oParams);

System.Web.HttpContext.Current.Cache.Add(messagetext,
oTextListHolder,
null,
System.DateTime.MaxValue,
new System.TimeSpan(0, 30, 0),
System.Web.Caching.CacheItemPriority.Normal,
null);

}
//-----end of cache--------

cheers
ramesh
Left by ramesh on Mar 20, 2006 3:02 AM

Your comment:
 (will show your gravatar)


Copyright © Mohammad Azam | Powered by: GeeksWithBlogs.net