Syntax Error
Syntax Error

Reputation: 1640

CrystalReportViewer forgetting parameters

I've got a CrystalReportViewer which is showing a report. I'm passing two parameters to it which change the data. This works, unless you try to drill down into the report further, at which point it resets to the default parameters.

It's a bit like the reportviewer element refreshes itself and forgets everything. I did have a page_init method which attempted to push the values back in but I was getting object reference errors so had probably done it wrong.

What else should I try? Please see code below:

 protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["ReportSource"] != null)
        {
            CrystalReportViewer1.ReportSource = (ReportDocument)Session["ReportSource"];
        }
        else
        {
            ReportDocument reportDocument = new ReportDocument();
            reportDocument.Load(Server.MapPath("~/Report/In Out.rpt"));
            reportDocument.SetDatabaseLogon("user", "pass" ,@"server", "db"); //removed credentials for stackoverflow!
            CrystalReportViewer1.ReportSource = reportDocument;
            Session["ReportSource"] = reportDocument;

        }
    }

    protected void butReport_Click(object sender, EventArgs e)
    {
        ReportDocument reportDocument = new ReportDocument();
        reportDocument.Load(Server.MapPath("./Report/In Out.rpt"));
          reportDocument.SetDatabaseLogon("user", "pass" ,@"server", "db"); //removed credentials for stackoverflow!    
        ///Load Session variables with values from web-controls
        Session["@FromDate"] = ConvertToDateTime(Request.Form["StartDate"]);
        Session["@ToDate"] = ConvertToDateTime(Request.Form["EndDate"]);
        reportDocument.SetParameterValue("Start", Session["@FromDate"]);
        reportDocument.SetParameterValue("End", Session["@ToDate"]);
        CrystalReportViewer1.ReportSource = reportDocument;
    }

My CR is embedded as follows

<div align="center">
            <asp:TextBox ID="StartDate" runat="server" type="date" placeholder="e.g. 31/12/2014" ></asp:TextBox> 
            <asp:TextBox ID="EndDate" runat="server" type="date" placeholder="e.g. 31/12/2014" ></asp:TextBox>
            <asp:button runat="server" id="Submit" type="button" value="Show Report" Text="View Report" onclick="butReport_Click" /></div>

        <div>
<CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" 
                   AutoDataBind="True" EnableDatabaseLogonPrompt="False" 
                   GroupTreeImagesFolderUrl="" Height="940px" 
                   ToolbarImagesFolderUrl="" ToolPanelWidth="200px" Width="1411px" 
                   EnableParameterPrompt="False" ReuseParameterValuesOnRefresh="True" 
                   ReportSourceID="CrystalReportSource1" />
               <CR:CrystalReportSource ID="CrystalReportSource1" runat="server">
                   <Report FileName="Report\In Out.rpt">
                   </Report>
               </CR:CrystalReportSource>
</div>

Upvotes: 0

Views: 612

Answers (1)

Nyerguds
Nyerguds

Reputation: 5639

If you want to save that loaded Crystal Reports document in the session, you should do so after you initialize it, with the parameters. At the moment that strange refresh thing happens it's already too late, and the code you have for reloading it in the page init notably doesn't contain that parameter loading code.

protected void Page_Load(object sender, EventArgs e)
{
    if (Session["ReportSource"] != null)
        CrystalReportViewer1.ReportSource = (ReportDocument)Session["ReportSource"];
 }

protected void butReport_Click(object sender, EventArgs e)
{
    ReportDocument reportDocument = new ReportDocument();
    /* your code to load the document and assign it as datasource to the CrystalReportViewer */
    // Assign your result to the session:
    Session["ReportSource"] = reportDocument;
}

Another option is to simply split out everything in the butReport_Click to a new function, and call that from both the button click and the init:

protected void Page_Load(object sender, EventArgs e)
{
    PrepareCR();
}

protected void butReport_Click(object sender, EventArgs e)
{
    PrepareCR();
}

protected void PrepareCR()
{
    ReportDocument reportDocument = new ReportDocument();
    /* your code to load the document and assign it as datasource to the CrystalReportViewer */

}

Note, I'm not sure how the resource management of these objects in ASP.NET goes; I know in my Windows app (which converted them to PDF) I had to be very careful to close the doc afterwards to avoid getting errors, so the first method might be preferable in that aspect, since it opens the document only once and retains that object.

Also, doesn't the embedding asp xml already load the document on page load, again without these required parameters?

Upvotes: 1

Related Questions