将rdlc转换为pdf后,ReportViewer无法显示

时间:2015-12-01 20:49:09

标签: c# asp.net pdf visual-studio-2012 rdlc

我有一个报告rdlc,我只是从vs 2010迁移到vs 2012,我正在使用一种方法将rdlc文件转换为pdf文件并通过电子邮件发送报告。

电子邮件正在成功发送,但该报告却抛出了这个错误:“Referencia a objeto a objeto no establecida como instancia de un objeto”。

我想当我将rdlc文件转换为pdf然后无法读取报告时发生了一些事情。

这是我用来将rdlc转换为pdf的方法。

private string ExportReportToPDF(string reportName)
    {
        try
        {
            Warning[] warnings;
            string[] streamids;
            string mimeType;
            string encoding;
            string filenameExtension;
            byte[] bytes = ReportViewer1.LocalReport.Render(
               "PDF", null, out mimeType, out encoding, out filenameExtension,
                out streamids, out warnings);

            string filename = Path.Combine(Path.GetTempPath(), reportName)+".pdf";
            using (var fs = new FileStream(filename, FileMode.Create))
            {
                fs.Write(bytes, 0, bytes.Length);
                fs.Close();
            }

            return filename;
        }
        catch (Exception err)
        {
           // Throws error
        }

    }

之后我只是将文件添加到邮件中并发送。

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

我不知道为什么但是在获取要转换为pdf的字节后,某些事情使得reportviewer无法正确呈现。

在尝试了不同的解决方法之后我发现我只需要再次刷新我的reportviewer所以在调用ExportToPDF方法并发送邮件后我刚刚添加了以下行:

ReportViewer1.LocalReport.Refresh();