Skip to Content

Continuing with my Persisting Crystal Reports .NET web applications – I on the same topic where we discussed ViewState and Session, with this blog I would like to shed some light on other approaches like Cache and Application State. Though I never wanted to make two parts of the same blog; had that been one, the length would have made it look big and probably a humdrum. Coming back to the point let us start with Cache and then Application State.

 

  • Cache:

Cache is server based approach used for persistence. It uses Server’s Memory as its storage location. The lifetime of caching depends upon the expiration policy set explicitly but may possibly be released early if the server memory becomes scarce.

It can be used for Reports object(s). There are two ways of persisting the report encapsulated within ReportDocument or ReportClientDocument object model using Cache:

  1. Instantiate the report and then assign it to the Cache object.
  2. Instantiate a version of the report class that implements the ICachedReport interface.

If report has high shareability then Cache is the best choice. It is worth knowing that cache does not understand the content of the items stored and only hold the references to those objects.


string cachedReports;
if (Session["ReportsName"] == null)
{
     string reportName = System.Guid.NewGuid().ToString();
     Session.Add("ReportsName", reportName);
     cachedReports = Session["ReportsName"].ToString();
}
else
{
     cachedReports = Session["ReportsName"].ToString();
}
if (Cache[cachedReports] == null)
{
     ReportDocument rd = new ReportDocument();
     rd.Load(Server.MapPath("Employee Sales.rpt"));
     Cache.Insert(cachedReports, rd, null, DateTime.MaxValue, TimeSpan.FromMinutes(10));
     CrystalReportViewer1.ReportSource = rd;
}
else
{
     CrystalReportViewer1.ReportSource = Cache[cachedReports];
}

The code looks little complex, but go with it only if you would like to keep the session separate, for each user would like to see the cached copy of their report not the one cached by some user.

  • Application State:

Application State, sever based approach used for persistence, is knows as a data repository to all the classes in an ASP.NET application. This data is global to all the users as its scope throughout the ASP.NET application. It is believed to be much faster than database in storing and retrieving the information.

It can be used to persist the report encapsulated within ReportDocument or ReportClientDocumentobject model. The implementation is no different than Cache. Large amount of data in it can fill up server memory quickly taking toll on performance as this data never times out or is removed. It’s data is lost on restarting the application or the server or until the applicaiton domain refreshes itself.

Comparing all the approaches would be like comparing apples to oranges as each has its own pros and cons. If you ask my favorite, it’s Session, easy to implement and widely used. You don’t have to worry about maintaining separate copies of the same report for different users, Session takes care of all. I don’t want to endorse the usage of the Session, if your project requirement demands something else go with it. Those who were not familiar with it can start making changes to the code and can experience a considerable change in the way Crystal Reports behaves.

Additional Resources:

Cache

  1. ASP.NET Caching Overview
  2. Caching ASP.NET Pages
  3. Caching Application Data

ApplicationState

  1. ASP.NET Application State Overview
To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply