Using Reports locally in ASP.NET 2.0

I was asked recently to develop reports for an internal ASP.NET app, after doing some research found that the best solution was to use the ASP.NET ReportViewer control set in LocalMode.

One of my big restrictions was the inability to upgrade from SQL Server 2000 to 2005, which meant avoiding I needed to avoid MS SQL Reporting Services. After much research into third party reporting components such as the Telerik Reporting Control as well as the SharpDevelop Reports, in the end I found a CodeProject article on Using the ASP.NET 2.0 ReportViewer in Local Mode which sounded perfect for my requirements.

Another requirement was to have the reports emailed out to respective contacts within the system, with PDF being the preferred format. My initial thought was to export to a temporary file on the system and attach it to the system generated email.

Found some code on the MSDN Forums about Printing Local Reports in ASP.NET which contained a useful looking ReportPrinterHelper class. Reading the code I noticed that it called the ReportViewer’s LocalReport.Render() function, and passed “PDF” as a parameter and also returned a byte[] array. An aha! moment struck, I could create a MemoryStream from the Byte[] array and attach that to an email as a PDF file.

Here’s my new RenderToPDFBytes function:

public static byte[] RenderToPDFBytes(ReportViewer reportViewer, ExportType exportType) 
{ 
  if (exportType == ExportType.Pdf) 
  { 
    Warning[] warnings; 
    string mimeType, encoding, fileNameExtension; 
    string[] streams; 
    byte[] pdfContent = reportViewer.LocalReport.Render("PDF", null, out mimeType, out encoding, out fileNameExtension, out streams, out warnings); 
    return pdfContent; 
  } 
  else if (exportType == ExportType.Xls) 
  { 
    // Implement if needed, just change PDF to XLS 
  } 
  return new byte[0]; 
}

Just place that into your ReportPrinterHelper class and reference it in your code as follows:

// Render our ReportViewer control to PDF format as bytes 
byte[] data = ReportPrinterHelper.RenderToPDFBytes(rptViewer1, ExportType.Pdf); 
// Transform our byte[] into a format suitable for attaching to an email. 
// Eg: Either in memory, or as a file on the system. 
MemoryStream ms = new MemoryStream(data); 
// Now to email it out as an attachment 
try 
{ 
  MailMessage mm = new MailMessage(); 
  mm.From = new MailAddress("noreply@example.com", "Ex Corp"); 
  mm.To.Add(new MailAddress("toaddress@example.com", "Bob Code")); 
  mm.Subject = "Daily Random Report"; 
  mm.Body = "Please find attached your Daily Report";    

  // Add our attachment to the MailMessage 
  mm.Attachments.Add(new Attachment(ms, "random.pdf", "application/pdf"));    

  // Set smtp.example.com to your relevant SMTP Host 
  SmtpClient smtp = new SmtpClient("smtp.example.com"); 
  smtp.Send(mm); 
} 
catch (System.Exception e) 
{ 
  // catch error and do something with it. 
}

Note: The ReportViewer control is only available as part of Visual Studio 2005 and is not a part of ASP.NET. As such, you’ll need Visual Studio installed to work with it.

Comments are closed.