How to Use RDLC Local Report in ASP.Net to Download in Doc PDF and Excel Format

This article demonstrates how to use a RDLC local report to get various downloadable file formats of reports, such as a Word or Excel document or a PDF.

Prerequisites: VS2010, SQL Server 2005/08

Step 1: Test Data

The following is my test data and also Stored Procedure to fetch the data for the report.


  1. -- =============================================  
  2. --  EXEC USP_GETEmployeeDetails  
  3. -- =============================================  
  4. ALTER PROCEDURE [dbo].[USP_GETEmployeeDetails]  
  5. AS  
  6. BEGIN  
  7.       
  8.     SELECT     SrID              
  9.         ,  EmployeeNumber              
  10.         ,  LoginID              
  11.         ,  JobTitle              
  12.         ,  BirthDate              
  13.         ,  MaritalStatus              
  14.         ,  Gender              
  15.         ,  HireDate              
  16.         ,  SalariedFlag              
  17.         ,  VacationHours              
  18.         ,  SickLeaveHours            
  19.     FROM    Employee           
  20. END 

Step 2

Create a new ASP.NET Empty Web Application.



Step 3

Add a new DataSet from the Data templates.



Step 4

Here add a new DataTable into a Dataset as shown below.



Step 5

Add columns to the DataTable and name each column the same as used for the Stored Procedure.



Finally the Data Table is ready, having the required columns in it.



Step 6

Add a new Report file (.rdlc) from the Reports templates.



The RDLC report has the default view as below:



Step 7

In the Report Data click and new button and select DataSet. And then select the appropriate DataSet. After selecting the DataSet, the columns appear in the right tab.



After adding the DataSet, the report data is as below:



Step 8

Right-click on Report Page and select the Insert command. Select Table from the available tools.



Step 9

Binding DataColumns from the DataSet in the Table Control. Right-click on the dynamic row and select the appropriate column from DataColumns as shown below.



After adding DataColumns the Report Page is as in the following:



Step 10

Up to this step we have completed the report design.

Add new WebPage



Step 11

The WebPage has the following script and it will look as in the following image:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="frmReport.aspx.cs" Inherits="ReportApplication.frmReport" %>  
  2.   
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head runat="server">  
  5.     <title></title>  
  6. </head>  
  7. <body>  
  8.     <form id="form1" runat="server">  
  9.     <center>  
  10.         <h2>  
  11.             Employee Report</h2>  
  12.         <table width="60%" border="1">  
  13.             <tr>  
  14.                 <td>  
  15.                     <asp:DropDownList ID="ddlFileFormat" runat="server">  
  16.                         <asp:ListItem Text="PDF" Value=".pdf"></asp:ListItem>  
  17.                         <asp:ListItem Text="WORD" Value=".doc"></asp:ListItem>  
  18.                         <asp:ListItem Text="EXCEL" Value=".xls"></asp:ListItem>  
  19.                     </asp:DropDownList>  
  20.                 </td>  
  21.                 <td>  
  22.                     <asp:Button ID="btnDownload" runat="server" Text="Download" OnClick="btnDownload_Click" />  
  23.                 </td>  
  24.             </tr>  
  25.         </table>  
  26.     </center>  
  27.     </form>  
  28. </body>  
  29. </html> 



The WebPage has the following C# code:

  1. namespace ReportApplication  
  2. {  
  3.     public partial class frmReport : System.Web.UI.Page  
  4.     {  
  5.         protected void Page_Load(object sender, EventArgs e)  
  6.         {  
  7.         }  
  8.  
  9.         #region " [ Button Event ] "  
  10.         protected void btnDownload_Click(object sender, EventArgs e)  
  11.         {  
  12.             // select appropriate contenttype, while binary transfer it identifies filetype  
  13.             string contentType = string.Empty;  
  14.             if (ddlFileFormat.SelectedValue.Equals(".pdf"))  
  15.                 contentType = "application/pdf";  
  16.             if (ddlFileFormat.SelectedValue.Equals(".doc"))  
  17.                 contentType = "application/ms-word";  
  18.             if (ddlFileFormat.SelectedValue.Equals(".xls"))  
  19.                 contentType = "application/xls";  
  20.   
  21.             DataTable dsData = new DataTable();  
  22.             dsData = getReportData();  
  23.   
  24.             string FileName = "File_" + DateTime.Now.ToString("ddMMyyyyhhmmss") + ddlFileFormat.SelectedValue;  
  25.             string extension;  
  26.             string encoding;  
  27.             string mimeType;  
  28.             string[] streams;  
  29.             Warning[] warnings;  
  30.   
  31.             LocalReport report = new LocalReport();  
  32.             report.ReportPath = Server.MapPath("~/rptEmployee.rdlc");  
  33.             ReportDataSource rds = new ReportDataSource();  
  34.             rds.Name = "DataSet1";//This refers to the dataset name in the RDLC file  
  35.             rds.Value = dsData;  
  36.             report.DataSources.Add(rds);  
  37.   
  38.             Byte[] mybytes = report.Render(ddlFileFormat.SelectedItem.Text, null,  
  39.                             out extension, out encoding,  
  40.                             out mimeType, out streams, out warnings); //for exporting to PDF  
  41.             using (FileStream fs = File.Create(Server.MapPath("~/download/") + FileName))  
  42.             {  
  43.                 fs.Write(mybytes, 0, mybytes.Length);  
  44.             }  
  45.   
  46.             Response.ClearHeaders();  
  47.             Response.ClearContent();  
  48.             Response.Buffer = true;  
  49.             Response.Clear();  
  50.             Response.ContentType = contentType;  
  51.             Response.AddHeader("Content-Disposition""attachment; filename=" + FileName);  
  52.             Response.WriteFile(Server.MapPath("~/download/" + FileName));  
  53.             Response.Flush();  
  54.             Response.Close();  
  55.             Response.End();  
  56.         }  
  57.         #endregion  
  58.  
  59.         #region " [ Get report Data ] "  
  60.         private DataTable getReportData()  
  61.         {  
  62.             DataSet dsData = new DataSet();  
  63.             SqlConnection sqlCon = null;  
  64.             SqlDataAdapter sqlCmd = null;  
  65.   
  66.             try  
  67.             {  
  68.                 using (sqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString))  
  69.                 {  
  70.                     sqlCmd = new SqlDataAdapter("USP_GETEmployeeDetails", sqlCon);  
  71.                     sqlCmd.SelectCommand.CommandType = CommandType.StoredProcedure;  
  72.   
  73.                     sqlCon.Open();  
  74.                     sqlCmd.Fill(dsData);  
  75.   
  76.                     sqlCon.Close();  
  77.                 }  
  78.             }  
  79.             catch  
  80.             {  
  81.                 throw;  
  82.             }  
  83.             return dsData.Tables[0];  
  84.         }  
  85.         #endregion  
  86.     }  

Step 12

When using a RDLC Local report, it is necessary to add the following assemblies.



Step 13

Finally build and run the project.

The results are as below.

1. PDF downloadable report file



2. DOC downloadable report file



3. Excel downloadable report file



The following are the files stored in the download folder:



Step 14: Deployment of RDLC report on IIS

Most of the time the hosting server is not updated with the Microsoft Reporting Package and then we receive the following error after deployment.

Could not load file or assembly 'Microsoft.ReportViewer.Common, Version=11.0.0.0

This error occurs since required assemblies are not present in the GAC's assembly folder.

Remedy: When deploying the project add the following assemblies to the bin folder:

  1. Microsoft.ReportViewer.Common.dll
  2. Microsoft.ReportViewer.ProcessingObjectModel.dll
  3. Microsoft.ReportViewer.WebForms.dll
  4. Microsoft.ReportViewer.WinForms.dll (not required for web application)

For more detailed code and database script information download the source code attached.


Similar Articles