Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
saurabh_pathak
Active Contributor
0 Kudos

In a recent case I discovered another reason why it is important to store the ReportDocument in session. Not only does it make application more efficient, but it can help you prevent errors while printing.

I deployed my website on a production server making sure that machine had runtimes and everything is configured correctly. Clicking on Print button of Crystal Report Viewer panel resulted in following error:

I checked the Printer & Network Settings & PrintControl.cab for any missing dll but to no avail. Continuously looking at Temp directory for any changes, I found that report was able to create a temp copy whenever it was called (boReportDocument.Load(“reportname.rpt”)) and also noticed that every event say Print, Export or moving to Last page causes postbacks, during postbacks it creates new temp files which helped me conclude that something was wrong with these postbacks.

Later I stored the ReportDocument object in Session & tried printing from panel.

Session.Add(“Report”, boReportDocument);
CrystalReportViewer.ReportSource = Session[“Report”];

The expanded version of the code may look like following:

If(Session[“Report”]==null)
{
boReportDocument = new ReportDocument();
boReportDocument.Load(“reportname.rpt”);
Session.Add(“Report”, boReportDocument);
}
CrystalReportViewer.ReportSource = Session[“Report”];

This time I got the output without any error and new temp files were not created.

Question will arise why do I store ReportDocument object in Session?

Load() does not actually load the report file that is reference in the method call. It creates a temp copy and uses it to load the report into memory. If we don’t store the report somewhere the data is gone on the postback which results in the error.

Another question that may arise why loading reports from disk on each postback is a bad idea?

The answer is very simple; it’s because of following:

  • High I/O disk time to load report.
  • High database usage as the query is re-run on each page navigation (a particular problem for long running reports).
  • Increased network traffic between app server and database etc... 
4 Comments
Labels in this area