Search code examples
visual-studio-2010reporting-servicesasp.net-3.5

SQL Reporting Services report only loads on second click


I have a local .rdlc report rigged to show on a button click, but for some reason the report only shows up on the 2nd button click event. I have no idea why the report doesn't show on the first button click... This is the function I call on the click event of the button.

private void ShowReport(string accountingCompanyId, string companyId, string approvalUnitId, DateTime startDate, DateTime finishDate, string supplierId,
                    string documentNumber, string documentType, string documentState, string costCenterId, string chargingKeyId,
                    string dim1Value, string dim1Description, string dim1Id, string dim2Value, string dim2Description, string dim2Id,
                    string dim3Value, string dim3Description, string dim3Id, bool showDetails) {

        //this.ReportViewer1.Reset();

        //Set report mode for local processing.
        this.ReportViewer1.ProcessingMode = ProcessingMode.Local;

        ISettingsReader settingsReader = SettingsReaderFactory.Instance.CreateSettingsReader();
        this.ReportViewer1.LocalReport.ReportPath = settingsReader.GetSetting("ReportViewer", "FinancialReportPath" + (showDetails ? "" : "Small"), true, null);

        ReportsBL reports = new ReportsBL();

        // Clear out any previous datasources.
        this.ReportViewer1.LocalReport.DataSources.Clear();

        // Load the company dataSource.
        DataTable company = reports.GetCompanyDataSet(accountingCompanyId).Tables[0];
        ReportDataSource dataSourceCompany = new ReportDataSource(company.TableName, company);
        this.ReportViewer1.LocalReport.DataSources.Add(dataSourceCompany);

        // Load the dataSource.
        DataTable report = reports.GetReportFinanceiroSmallDataSet(companyId, startDate, finishDate, chargingKeyId, costCenterId, documentNumber, documentType, dim1Value, dim2Value, dim3Value, dim1Id, dim2Id, dim3Id, supplierId, approvalUnitId, documentState, accountingCompanyId).Tables[0];
        ReportDataSource dataSourceReport = new ReportDataSource(report.TableName, report);
        this.ReportViewer1.LocalReport.DataSources.Add(dataSourceReport);

        this.ReportViewer1.LocalReport.Refresh();

        this.pnlReport.Visible = true;
    }

Strangely, if I uncomment the line this.ReportViewer.Reset(); then the report will never show up regardless of the number of clicks I generate... Does anybody know if this is normal? How can work around the problem? Thanks in advance,


Solution

  • After a lot of trial and error, I got it working by calling the databind() method on the the pageload event. After the pageload databinds (with no datasource set), the subsequent button click starts working as expected.

    I'm included the code in case somebody else encounters this error. (Would love to know why I need to databind in the pageload though...)

    Update 2

    I finally figured out the problem... Turns out the .rdlc was migrated from an old 2005 .rdl report, and that the new .rdlc contained old report parameters+sql which was somehow messing up the report loading. After I removed the unused report parameteres+sql everything started working perfectly... I've updated the code bellow to reflect what I'm now using in my project...

    protected void Page_Load(object sender, System.EventArgs e) {
    
    }
    
    protected void btGenStats_Click(object sender, System.EventArgs e) {
    
        ...
    
        this.ShowReport(accountingCompanyId, companyId, approvalUnitId, startDate, finishDate, supplierId, documentNumber, documentType, documentState,
                        costCenterId, chargingKeyId, dim1, dim1Desc, dim1Id, dim2, dim2Desc, dim2Id, dim3, dim3Desc, dim3Id, showDetails);
    }
    
    private void ShowReport(string accountingCompanyId, string companyId, string approvalUnitId, DateTime startDate, DateTime finishDate, string supplierId, string documentNumber, string documentType, string documentState, string costCenterId, string chargingKeyId, string dim1Value, string dim1Description, string dim1Id, string dim2Value, string dim2Description, string dim2Id, string dim3Value, string dim3Description, string dim3Id, bool showDetails) {
    
        this.ReportViewer1.Reset();
    
        //Set report mode for local processing.
        this.ReportViewer1.ProcessingMode = ProcessingMode.Local;
    
        ISettingsReader settingsReader = SettingsReaderFactory.Instance.CreateSettingsReader();
        string reportPath = settingsReader.GetSetting("ReportViewer", "FinancialReportPath"+( showDetails? "" : "Small" ), true, null);
        this.ReportViewer1.LocalReport.ReportPath = Server.MapPath(reportPath);
    
        ReportsBL reports = new ReportsBL();
    
        // Clear out any previous datasources.
        this.ReportViewer1.LocalReport.DataSources.Clear();
    
        // Load the company dataSource.
        DataTable company = reports.GetCompanyDataSet(accountingCompanyId).Tables[0];
        ReportDataSource dataSourceCompany = new ReportDataSource(company.TableName, company);
        this.ReportViewer1.LocalReport.DataSources.Add(dataSourceCompany);
    
        if (showDetails)
        {
            // Load the dataSource.
            DataTable report = reports.GetReportFinanceiroDataSet(companyId, approvalUnitId, startDate, finishDate, chargingKeyId, costCenterId, documentNumber, documentType, dim1Value, dim2Value, dim3Value, dim1Id, dim2Id, dim3Id, supplierId, documentState, accountingCompanyId).Tables[0];
            ReportDataSource dataSourceReport = new ReportDataSource(report.TableName, report);
            this.ReportViewer1.LocalReport.DataSources.Add(dataSourceReport);
        }
        else
        {
            // Load the dataSource.
            DataTable report = reports.GetReportFinanceiroSmallDataSet(companyId, approvalUnitId, startDate, finishDate, chargingKeyId, costCenterId, documentNumber, documentType, dim1Value, dim2Value, dim3Value, dim1Id, dim2Id, dim3Id, supplierId, documentState, accountingCompanyId).Tables[0];
            ReportDataSource dataSourceReport = new ReportDataSource(report.TableName, report);
            this.ReportViewer1.LocalReport.DataSources.Add(dataSourceReport);
        }
    
        this.pnlReport.Visible = true;
    }