0

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.

Brandon Moore
  • 8,590
  • 15
  • 65
  • 120
  • 1
    You should show us your SelectedIndexChanged event handler. When, where and how are you databinding the DropDownList? – Tim Schmelter Apr 24 '12 at 20:56
  • @TimSchmelter, From the question it looks like he is using that – Habib Apr 24 '12 at 20:57
  • 1
    @Habib.OSU: He is using what? I Haven't said that he should handle the SelectedIndexChanged event. I've said that he should show us this handler so that we can see what's going wrong. – Tim Schmelter Apr 24 '12 at 20:58
  • @BrandonMoore: Nothing obvious for me, but i've no experiences with CrystalReportViewer. Have you used the debugger to look if there's an exception raised or something similar? Side-note: `Data.ExecutSql` looks like a self-made DB-class, isn't it? Might not be a good idea in ASP.NET (in case you're using static connections etc) http://stackoverflow.com/questions/9705637/executereader-requires-an-open-and-available-connection-the-connections-curren/9707060#9707060 – Tim Schmelter Apr 24 '12 at 21:13
  • @TimSchmelter Thanks, I didn't know know that although the data access code that's in there now is just temporary as I'm just trying to make these dang reports show up correctly right now :). But... I still like the idea of encapsulating it; just sounds like I need the class that does that to open/close connections on each request. – Brandon Moore Apr 24 '12 at 21:23
  • @TimSchmelter Ok, I thought this was just a simple case of me not fully understanding how the flow of events works in asp.net, but when I just placed a label on my page to show the currently selected item it stays in sync properly. So I guess it is something to do with the Crystal Report Viewer :/ Not really sure what to try next. – Brandon Moore Apr 24 '12 at 21:30
  • @BrandonMoore: I do not wish to digress from the question, but open,close and dispose(using-statement) connections as soon as you're finished(f.e. in that method that uses it). A whole request is too long. And **never** use a static connection(least of all in ASP.NET since every request is a different thread). **Edit** As i've already mentioned, i'm not familiar with CrystalReportViewer so i'm not the right person here. – Tim Schmelter Apr 24 '12 at 21:32
  • 1
    @TimSchmelter Yeah, the data layer I'm ultimately going to be using fills business objects and returns them rather than returning a datareader. I just need to modify it to open/close the connection each time as you say. – Brandon Moore Apr 24 '12 at 21:53

1 Answers1

0

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.

Brandon Moore
  • 8,590
  • 15
  • 65
  • 120