【问题标题】:Check my SSRS report if it's empty from code如果代码中为空,请检查我的 SSRS 报告
【发布时间】:2012-06-28 15:01:58
【问题描述】:

我正在通过代码(C#)加载我使用 SSRS 完成的报告,但我需要检查报告是否为空!!!

我怎么能得到那个?! 我使用的代码是:

if (!string.IsNullOrEmpty(RptInstance.FileName))
            {
                string ReportName = RptInstance.FileName.Replace(".rpt", "");
                reportViewer.ServerReport.ReportPath = string.Format("{0}{1}", Settings.Default.ReportPath, ReportName);
                reportViewer.ServerReport.SetParameters(paramList);
                reportViewer.ServerReport.Timeout = Timeout;

                string mimeType, encoding, extension, deviceInfo;
                string[] streamIds = null;
                Warning[] warnings = null;
                deviceInfo = "<DeviceInfo><SimplePageHeaders>True</SimplePageHeaders></DeviceInfo>";

                byte[] bytes = reportViewer.ServerReport.Render(RptInstance.PDF ? "PDF" : "Excel", deviceInfo, out mimeType, out encoding, out extension, out streamIds, out warnings);
                RptInstance.State = true;
                //SaveData(Report.FileName, bytes, string.Format(@"C:\temp\{0}.{1}", ReportName, isPdf ? "pdf" : "xlsx"));

                SaveData(string.Format("{0}_{1}.{2}", ReportName, Guid.NewGuid(), RptInstance.PDF ? "pdf" : "xlsx"), bytes, DirectoryName);
            }

提前致谢

【问题讨论】:

  • 报告为空是什么意思?你的意思是你传递给你的数据集的参数没有记录吗?
  • @nunespascal 正是我的意思

标签: c# web-services ssrs-2008 reportviewer2008


【解决方案1】:

数据集是报表内部的。没有外部 API 可用于查找特定数据集针对给定报告参数返回的记录数。

有两种方法可以解决这个问题:

1 .使用LocalReport

在 C# 代码中创建您的数据集,检查它是否有行,然后将其作为数据源提供给 SSRS。

ReportDataSource ds = new ReportDataSource(dsName, data);
ReportGenerator gen = new ReportGenerator(data, dsName);
reportViewer.Reset();
reportViewer.LocalReport.DataSources.Add(ds);
reportViewer.LocalReport.DisplayName = displayName;
reportViewer.LocalReport.LoadReportDefinition(gen.GeneraReport());

参考:Dynamic Reports with Reporting Services

2 。以 CSV 格式下载您的报告。这将为您提供一组参数的每个 DataSource 的原始数据。在这里可以查看返回的行数。

【讨论】:

  • 使用第二种解决方案,将报告下载为 CSV,我怎么知道每个数据源的原始数据?!!,为了检查行数?!!提前致谢
  • 将您的报告另存为 CSV 文件,然后查看每个数据源的数据。计算 CSV 文件中的行数应该很简单。
【解决方案2】:

这是我的忍者做法。

将每个控件的隐藏属性设置为当DataSet没有记录时隐藏控件的表达式:

=Rownumber("TransactionsByAccountAndEffDate") = 0

在报表中,我还插入了一个 TextBox 并设置了它的 Hidden 属性,以便让人们知道报表何时没有数据:

=Rownumber("TransactionsByAccountAndEffDate") > 0

然后当我生成没有数据的报告时,所有控件都将被隐藏,这使得文件的大小变得很小:

在我用代码创建报告后,我简单地检查文件大小,看看它是否很小:

var length = new System.IO.FileInfo(path).Length;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多