Technology For Your Business For Your Life

Tuesday, September 13, 2011

Email Reporting Services Report as a pdf attachment from C# code

As a follow on from the last post a further requirement we had was for the client to be able to email the .rdlc report as an attachment in pdf format. Here is the code to do that...

     LocalReport localReport = new LocalReport();
            localReport.ReportPath = Server.MapPath("..\\Report\\report.rdlc");
            localReport.EnableExternalImages = true;
            ReportDataSource reportDataSource = new ReportDataSource("DataSet1", q.GetData(ID));
            localReport.DataSources.Add(reportDataSource);
      
          string reportType = "PDF";
          string mimeType;
          string encoding;
          string fileNameExtension;
          string deviceInfo =
    "<DeviceInfo>" +
    "  <OutputFormat>PDF</OutputFormat>" +
    "  <PageWidth>9.5in</PageWidth>" +
    "  <PageHeight>11in</PageHeight>" +
    "  <MarginTop>0.5in</MarginTop>" +
    "  <MarginLeft>1in</MarginLeft>" +
    "  <MarginRight>1in</MarginRight>" +
    "  <MarginBottom>0.5in</MarginBottom>" +
    "</DeviceInfo>";
          Warning[] warnings;
          string[] streams;
          byte[] renderedBytes;

          //Render the report
          renderedBytes = localReport.Render(
              reportType,
              deviceInfo,
              out mimeType,
              out encoding,
              out fileNameExtension,
              out streams,
              out warnings);
          MemoryStream s = new MemoryStream(renderedBytes);
          s.Seek(0, SeekOrigin.Begin);

            BLLEmailComponent email = new BLLEmailComponent();
            email.SendQuote(EmailAddress, s);   //this function call actually contains the code to send the email

Visit us at http://www.creativewebsolutions.co.za/.

No comments:

Post a Comment