【问题标题】:From C# when calling SSRS report only main report shows data sub report giving error从 C# 调用 SSRS 报告时,仅主报告显示数据子报告给出错误
【发布时间】:2015-11-30 15:19:13
【问题描述】:

我开发了一个 SSRS 报告,其中包含一个主报告和 3 个子报告。 我从 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# 调用时出现错误:

在指定位置找不到。请确认子报告已发布且名称正确。

我的代码:

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 中很好..

【问题讨论】:

  • 并且子报表控件(例如 Tablix)绑定到 Dataset1s 字段?是共享数据集吗,怎么配置的?
  • @JeremyThompson 没有每个报告都有它的查询和数据集...但是所有报告的数据集名称都是 Dataset1
  • @JeremyThompson 是的..但是在 SSRS 中我的报告工作正常,因为没有参数传递..查询是硬代码..查询是在执行期间在 C# 中构建的,由 dataadapter 填充的数据表是作为报告的数据源..对不起,如果我错了..我对这个 SSRS 很天真
  • @Sachu 在您的 RDL 中调用子报表时,请提供 绝对路径 而不是相对路径。 SubReport Properties > General > Use this report as subreport

标签: c# reporting-services ssrs-2008 subreport


【解决方案1】:

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

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.如果有任何参数传递,请确保它的名称也正确。

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

现在运行 C# 应用程序,它将显示带有所有子报告的主报告

【讨论】:

  • 我知道我们应该避免使用它来表示“谢谢”,但说真的,您在这里的解决方案解决了一个难题,也结束了今天和昨天的大量头疼。到目前为止,我能找到的关于如何部署子报表的唯一示例使用无参数的 SELECT 语句,并且没有考虑在非常繁忙的数据库中的可用性。
  • @Dan 很高兴看到我的回答对您有所帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-30
  • 1970-01-01
  • 1970-01-01
  • 2018-11-25
  • 2019-03-10
  • 2013-06-06
相关资源
最近更新 更多