Switching Data in ReportViewer in WinForms

I created winform for users to view many of the reports that I create for them. I have a drop-down list with the name of the report that launches the appropriate fields to display the parameters. As soon as they are filled, they click the "Submit" button and a report appears. This works the first time you tap the screen. They can change parameters, and ReportViewer works fine. Switch to another report and I get the following ReportViewer error:

An error occurred during local report processing.
An error has occurred during the report processing.
A data source instance has not been supplied for the data source "CgTempData_BusMaintenance".

Regarding the process I'm using:

  • I set the reportName(string) physical name of the RDLC.
  • I set dataSource(string) as the name of the data source
  • I populate a generic DataTable with data to run a report.
  • Make ReportViewer Visible
  • Set LocalReport.ReportPath = "Reports\\" = reportName;
  • Clean LocalReport.DataSources.Clear()
  • LocalReport.DataSources.Add(new ReportDataSource(dataSource, dt));
  • RefreshReport() ReportViewer.

, ReportViewer:

/// <summary>
/// Builds the report.
/// </summary>
private void BuildReport()
{
    DataTable dt = null;
    ReportingCG rcg = new ReportingCG();

    if (reportName == "GasUsedReport.rdlc")
    {
        dataSource = "CgTempData_FuelLog";
        CgTempData.FuelLogDataTable DtFuelLog = rcg.BuildFuelUsedTable(fromDate, toDate);
        dt = DtFuelLog;
    }
    else if (reportName == "InventoryCost.rdlc")
    {
        CgTempData.InventoryUsedDataTable DtInventory;
        dataSource = "CgTempData_InventoryUsed";
        DtInventory = rcg.BuildInventoryUsedTable(fromDate, toDate);
        dt = DtInventory;
    }
    else if (reportName == "VehicleMasterList.rdlc")
    {
        dataSource = "CgTempData_VehicleMaster";
        CgTempData.VehicleMasterDataTable DtVehicleMaster = rcg.BuildVehicleMasterTable();
        dt = DtVehicleMaster;
    }
    else if (reportName == "BusCosts.rdlc")
    {
        dataSource = "CgTempData_BusMaintenance";
        dt = rcg.BuildBusCostsTable(fromDate, toDate);
    }

    // Setup the DataSource
    this.reportViewer1.Visible = true;
    this.reportViewer1.LocalReport.ReportPath = "Reports\\" + reportName;
    this.reportViewer1.LocalReport.DataSources.Clear();
    this.reportViewer1.LocalReport.DataSources.Add(new ReportDataSource(dataSource, dt));
    this.reportViewer1.RefreshReport();
}

, ? ?

+3
1

. : reportViewer1.Reset(); .

+2

Source: https://habr.com/ru/post/1745865/


All Articles