1985571 - How to use sessions in web applications using the Crystal Reports viewer (the complete code)

SAP Knowledge Base Article - Public

1985571 - How to use sessions in web applications using the Crystal Reports viewer (the complete code)

Symptom

  • Issues with muti-page report in web applications when the report is not in session:
    • Database logon prompts.
    • Second page of a report is blank.
    • First page of a report is blank.
    • Report does not print without database logon prompts / errors.
    • Report does not export without database logon prompts / errors.
    • And more...

Environment

  • SAP Crystal Reports, developer version for Microsoft Visual Studio
  • Microsoft Visual Studio 2010 / 2012

Cause

  • Almost any action on a report displayed in the web viewer will cause a postback.
  • This includes paging, drilling, zooming, searching, printing, exporting and more.
  • If a report is not in session, the database logon information is lost leading to errors and unanticipated behaviors.

Resolution

  • Below is a complete code that describes how to ensure the report is in session:
public partial class Reports : RAPPBasePage
{
    private ReportDocument crReportDocument;
    protected override void Page_Init(object sender, EventArgs e)
    {
        //connect method to master event
        base.Page_Init(sender, e);
    }
    protected override void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load(sender, e);
        if (IsPostBack) // post back event, check if report is in session if it is view it.
        {
            crReportDocument = ReportDocument)Session["Report"];
            // Now send the report to the viewer
            CrystalReportViewer1.ReportSource = crReportDocument;
        }
    }
    protected void butReport_Click(object sender, EventArgs e)
    {
  // code based on customer's examples
  If(Session["Report"]==null) // Report is not in session (previously loaded) so load report, set params, view and place in session
  {
            crReportDocument = new ReportDocument();
            crReportDocument.Load(Server.MapPath("MyTestReport.rpt"));
            crReportDocument.SetDatabaseLogon("User", "Password", "Server", "Database");
            crReportDocument.SetParameterValue("LocationId", fwsuser.currentLocation);
            Session.Add(“Report”, crReportDocument);
   CrystalReportViewer.ReportSource = crReportDocument;
  }
  else // Report is already loaded and in session so use it also means we never reload the report
        {
            crReportDocument = (ReportDocument)Session["Report"];
   // Now send the report to the viewer
            CrystalReportViewer1.ReportSource = crReportDocument;         
        }
  //// How I would do it to so report is reloaded when ever button is pressed (IE Refreshing report, or load a different report if option
present)
  // crReportDocument = new ReportDocument();
        //  crReportDocument.Load(Server.MapPath("Report_To_Load"));
        //  crReportDocument.SetDatabaseLogon("UserID", "UserPassword", "ServerName", "DatabaseName");
        //  crReportDocument.SetParameterValue("LocationId", fwsuser.currentLocation);
        //  Session.Add(“Report”, crReportDocument);
  // CrystalReportViewer.ReportSource = crReportDocument;
    }
    protected void Page_UnLoad(object sender, EventArgs e)
    {
        try
        {
            crReportDocument.Close();
            crReportDocument.Dispose();
        }
       catch { }
    }
}

Keywords

sesion crvs cr4vs dhtml log on empty blank white error multipage many page , KBA , BI-DEV-NET-SDK , .NET SDK / COM SDK , How To

Product

Crystal Reports 2008 V1 ; SAP Crystal Reports, developer version for Microsoft Visual Studio ; SAP Crystal Reports, version for Visual Studio .NET 2005 ; SAP Crystal Reports, version for Visual Studio .NET 2008