Question

I've got a dropdown, a label, and a CR Viewer on my page. When I select a report from the DD, I update the label to show the currently selected report and I update the CRV to show the report.

The label updates fine and I just put it there as a test to make sure other controls were updating properly. The CRV, on the other hand, is always one request behind. I pick a report and it doesn't show up. I pick another report and then the one I previously picked shows up.

The code posted below was from before I added the label, but nothing else is changed.

using System;
using DataAccess;
using CrystalDecisions.CrystalReports.Engine;

namespace Reporting
{
    public partial class CRViewer : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;
            ReportDropDown.Items.Add("Select a report");

            var reports = Data.ExecutSql("select * from ngmasterdb..reports");
            while (reports.Read()) ReportDropDown.Items.Add(reports["Name"].ToString());
            reports.Close();
        }

        protected void ReportDropDown_SelectedIndexChanged(object sender, EventArgs e)
        {
            var reportInfo = Data.ExecutSql("select * from ngmasterdb..reports where Name = '" + ReportDropDown.SelectedValue.Replace("'", "''") + "'");

            try
            {
                ReportDocument rptdoc = new ReportDocument();

                if (!reportInfo.Read()) return;
                var file = reportInfo["ReportFile"].ToString();
                if (file == null || file.Trim() == "") return;

                ReportSource.Report.FileName = file;
                CrystalReportViewer1.RefreshReport();
            }
            finally
            {
                reportInfo.Close();
            }
        }
    }
}

I believe the only thing of interest in the aspx is that AutoPostBack is set to true for the DropDown control. If you'd still like to see the aspx though let me know and I'll post it.

Was it helpful?

Solution

Originally I was basically doing this:

ReportSource.Report.FileName = rptFileName;
CrystalReportViewer1.ReportSource = ReportSource;

When I got rid of ReportSource (a CrystalDecisions.Web.CrystalReportSource object) and did this instead:

CrystalReportViewer1.ReportSource = rptFileName;

Then it started behaving correctly. I had tried this approach before (or at least I feel confident that I did though it would seem otherwise now...) but was getting some kind of errors regarding the file path.

I don't know why I got the errors I did before when I tried this, and I still don't know why the control wouldn't behave correctly even using the method I was trying so if anyone has any info on that feel free to clue me in.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top