从 C# 调用 SSRS 报告时仅主报告显示数据子报告给出错误

From C# when calling SSRS report only main report shows data sub report giving error

我开发了一个包含主报表和 3 个子报表的 SSRS 报表。 我正在从 C# 调用此报告。 我只知道怎么绑定主rdlc和数据集

我为此使用下面的代码

SqlDataAdapter dataAdapter = new SqlDataAdapter(sqlcomm);
dataAdapter.Fill(dataset);
this.reportViewer1.LocalReport.ReportPath = Application.StartupPath + "\sale_dept.rdl";
 this.reportViewer1.LocalReport.DataSources.Clear();
 this.reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("DataSet1", dataset.Tables[0]));
this.reportViewer1.RefreshReport();

当我 运行 exe 时,我得到的报表查看器充满了主报表,但 3 个子报表显示错误,因为我没有为这些子报表指定数据源

  1. 主报表和其他子报表之间没有参数传递
  2. 主报表和所有子报表的数据集名称默认为DataSet1

请指导我将子报告与适当的查询数据集表绑定。 我完全被困在这里了。

已编辑

我用 1 个子报表更改了我的项目。

在 SSRS 中,它在(BIDS)编辑器中工作正常,但从 C# 调用时出现错误:

Could not be found at the specified location. Please verify that the subreport has been published and that the name is correct.

我的代码:

subreportevenhandler according to this question

question for subreport event handler

 SqlDataAdapter dataAdapter = new SqlDataAdapter(sqlcomm);
    dataAdapter.Fill(dataset);
    this.reportViewer1.LocalReport.ReportPath = Application.StartupPath + "\sale_dept.rdl";
     this.reportViewer1.LocalReport.DataSources.Clear();
this.reportViewer1.LocalReport.SubreportProcessing +=
                new SubreportProcessingEventHandler(addsubreport);
     this.reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("DataSet1", dataset.Tables[0]));
    this.reportViewer1.RefreshReport();




void addsubreport(object sender, SubreportProcessingEventArgs e)
        {
            SqlConnection conn = new SqlConnection(source);
            DataSet dataset = new DataSet();
            conn.Open();

           SqlCommand sqlcomm = new SqlCommand( "Query for subreport", conn);

           SqlDataAdapter dataAdapter = new SqlDataAdapter(sqlcomm);
           dataAdapter.Fill(dataset);

           e.DataSources.Add(new ReportDataSource("DataSet1", dataset.Tables[0]));
        }

我仍然收到子报表错误 我将所有 .rdl 文件移动到 C# bin 文件夹..

主报表正确显示数据。在 SSRS 中很好..

我发现了问题。我将其作为答案发布,因为它可能会在将来对某人有所帮助。

SubreportProcessingEventHandler 只会为 .rdlc 子报表触发。 在我的项目主报表和所有子报表中都是 .rdl 扩展名。 所以我所做的唯一改变是转到命令提示符并将子报表扩展重命名为 .rdlc

例如:- ren discount.rdl discount.rdlc

然后相应地附上子报告的数据集。

代码如下

SqlDataAdapter dataAdapter = new SqlDataAdapter(sqlcomm);
dataAdapter.Fill(dataset);
this.reportViewer1.LocalReport.ReportPath = Application.StartupPath + "\main_rpt.rdl";
this.reportViewer1.LocalReport.DataSources.Clear();
this.reportViewer1.LocalReport.SubreportProcessing +=
new SubreportProcessingEventHandler(addsubreport);
this.reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("DataSet1", dataset.Tables[0]));
this.reportViewer1.RefreshReport();


void addsubreport(object sender, SubreportProcessingEventArgs e)
{

SqlCommand sqlcomm = new SqlCommand();
SqlDataAdapter dataAdapter = new SqlDataAdapter();
DataSet dataset = new DataSet();

Switch(e.ReportPath)
{
case "subreport1":
   sqlcomm = new SqlCommand( "Query for subreport one", conn);
   dataAdapter = new SqlDataAdapter(sqlcomm);
   dataAdapter.Fill(dataset);
   e.DataSources.Add(new ReportDataSource("DataSet for subreport1", dataset.Tables[0]));
   break;
case "subreport2":
   sqlcomm = new SqlCommand( "Query for subreport two", conn);
   dataAdapter = new SqlDataAdapter(sqlcomm);
   dataAdapter.Fill(dataset);
   e.DataSources.Add(new ReportDataSource("DataSet for subreport2", dataset.Tables[0]));
   break;
case "subreport3":
   sqlcomm = new SqlCommand( "Query for subreport three", conn);
   dataAdapter = new SqlDataAdapter(sqlcomm);
   dataAdapter.Fill(dataset);
   e.DataSources.Add(new ReportDataSource("DataSet for subreport3", dataset.Tables[0]));
   break;

 }

}

如果您有多个子报表,则需要切换。主要注意事项

  1. 所有子报告都应该有扩展名.rdlc

2.if 传递的任何参数确保它的命名也正确。

  1. 正确指定路径。最好将主报表和子报表放在同一个文件夹中。

现在 运行 C# 应用程序将显示主报表和所有子报表