gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 17:41:50 24 4
gpt4 key购买 nike

我开发了一个包含主报表和 3 个子报表的 SSRS 报表。我正在从 C# 调用此报告。我只知道如何将主rdlc与数据集绑定(bind)。

我使用下面的代码

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 个子报告显示错误,因为我没有为这些子报告指定数据源

enter image description here

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

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

已编辑

我用 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 文件夹..

enter image description here

主报告正确显示数据。在 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.如果传递了任何参数,请确保它的名称也正确。

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

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

关于c# - 从 C# 调用 SSRS 报告时仅主报告显示数据子报告给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32398499/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com