Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CrystalReportViewer forgetting parameters when drilling down

Status
Not open for further replies.

jf111

Programmer
Apr 4, 2016
15
GB
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:

Code:
     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 !
                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 !    
            ///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

Code:
  <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>
 
Just incase anyone finds this, I fixed it via the following 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();
reportDocument.Load(Server.MapPath("./Report/In Out.rpt"));
reportDocument.SetDatabaseLogon(credentials removed);

///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;
Session["ReportSource"] = reportDocument;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top