Skip to Content

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:

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… 
To report this post you need to login first.

4 Comments

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

  1. Andrew Baines
    If it’s not in the session, it will rerun the report every time you change a page. It used to be the case that you could never get past page 2 as well – not sure if that’s still the case.
    Without referring to the session, you’ve no state.
    (0) 
    1. Thomas Johnson
      Actually we found if you are using the CrystalReportSource, you need to increase the CacheDuration property and teh refetching will stop (untile the cache time is reached).
      (0) 
  2. Nikunj Polara

    Saurabh,

    I have created CR with on demand sub report and placed link of it on main report. I have created same code as you mentioned above but when I click on link of on demand sub report it gives me error “Invalid report file path.” also when I click on export or print button same error occur. Please suggest if I required some more code.

    Thanks and Regards,

    Nikunj Polara

    (0) 
    1. Ludek Uher

      Please create a Discussion in this SN Space. Also, just an FYI; addressing the author may not always pan out as they may not be working for SAP or follow SCN anymore. And unfortunately, Saurabh is one of those cases…

      – Ludek

      SCN Moderator

      (0) 

Leave a Reply