本文介绍了从C#调用SSRS报告时,仅主报告显示数据子报告给出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一个SSRS报告,其中包含一个主要报告和3个子报告.我正在从C#调用此报告.我只知道如何将主rdlc与数据集绑定.

I developed an SSRS report having a main and 3 subreport.I am calling this report from C#.I only know how to bind the main rdlc with the data set.

我为此使用下面的代码

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

when I run the exe i am getting the report viewer filled with main report but 3 subreport shows error because i didn't specify the DataSource for those subreports

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

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

Please guide me to bind the sub reports with appropriate query dataset tables.I am totally stuck here.

已编辑

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

I changed my project with 1 subreport.

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

In SSRS it is working fine in (the BIDS) editor but when calling from C# it is giving error:

我的代码:

subreportevenhandler according to this question

有关子报表事件处理程序的问题

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

Still I am getting error for subreportI moved all the .rdl file to C# bin folder..

主报告正确显示了数据.在SSRS中可以..

Main report is showing the data correctly. In SSRS its fine..

推荐答案

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

I found out the issue. I am posting it as answer because it may help someone in future.

SubreportProcessingEventHandler仅对.rdlc子报表触发.在我的项目主报告和所有子报告中,都是.rdl扩展名.因此,只有所做的更改进入了命令提示符,并将子报表扩展名重命名为.rdlc

The SubreportProcessingEventHandler will only get fired for .rdlc subreports.In my project main report and all subreports are .rdl extension.So only change I done was went to command prompt and renamed the subreport extension as .rdlc

例如:-ren discount.rdl discount.rdlc

然后相应地为子报表附加数据集.

Then attach the data-set for the sub-report accordingly.

请参见以下代码

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;

 }

}

如果您有多个子报告,则需要切换.注意事项

Switch is needed if you have more than one sub-report. Main point to notice

  1. 所有子报表均应具有扩展名.rdlc

2.如果有任何参数传递,请确保它也正确命名.

2.if any parameter is passing make sure it is also named correctly.

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

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

Now run the C# application it will show the main report with all sub-report

这篇关于从C#调用SSRS报告时,仅主报告显示数据子报告给出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 07:50