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.
-
-
-
- ALTER PROCEDURE [dbo].[USP_GETEmployeeDetails]
- AS
- BEGIN
-
- SELECT SrID
- , EmployeeNumber
- , LoginID
- , JobTitle
- , BirthDate
- , MaritalStatus
- , Gender
- , HireDate
- , SalariedFlag
- , VacationHours
- , SickLeaveHours
- FROM Employee
- 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:
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="frmReport.aspx.cs" Inherits="ReportApplication.frmReport" %>
-
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <center>
- <h2>
- Employee Report</h2>
- <table width="60%" border="1">
- <tr>
- <td>
- <asp:DropDownList ID="ddlFileFormat" runat="server">
- <asp:ListItem Text="PDF" Value=".pdf"></asp:ListItem>
- <asp:ListItem Text="WORD" Value=".doc"></asp:ListItem>
- <asp:ListItem Text="EXCEL" Value=".xls"></asp:ListItem>
- </asp:DropDownList>
- </td>
- <td>
- <asp:Button ID="btnDownload" runat="server" Text="Download" OnClick="btnDownload_Click" />
- </td>
- </tr>
- </table>
- </center>
- </form>
- </body>
- </html>
The WebPage has the following C# code:
- namespace ReportApplication
- {
- public partial class frmReport : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- }
-
- #region " [ Button Event ] "
- protected void btnDownload_Click(object sender, EventArgs e)
- {
-
- string contentType = string.Empty;
- if (ddlFileFormat.SelectedValue.Equals(".pdf"))
- contentType = "application/pdf";
- if (ddlFileFormat.SelectedValue.Equals(".doc"))
- contentType = "application/ms-word";
- if (ddlFileFormat.SelectedValue.Equals(".xls"))
- contentType = "application/xls";
-
- DataTable dsData = new DataTable();
- dsData = getReportData();
-
- string FileName = "File_" + DateTime.Now.ToString("ddMMyyyyhhmmss") + ddlFileFormat.SelectedValue;
- string extension;
- string encoding;
- string mimeType;
- string[] streams;
- Warning[] warnings;
-
- LocalReport report = new LocalReport();
- report.ReportPath = Server.MapPath("~/rptEmployee.rdlc");
- ReportDataSource rds = new ReportDataSource();
- rds.Name = "DataSet1";
- rds.Value = dsData;
- report.DataSources.Add(rds);
-
- Byte[] mybytes = report.Render(ddlFileFormat.SelectedItem.Text, null,
- out extension, out encoding,
- out mimeType, out streams, out warnings);
- using (FileStream fs = File.Create(Server.MapPath("~/download/") + FileName))
- {
- fs.Write(mybytes, 0, mybytes.Length);
- }
-
- Response.ClearHeaders();
- Response.ClearContent();
- Response.Buffer = true;
- Response.Clear();
- Response.ContentType = contentType;
- Response.AddHeader("Content-Disposition", "attachment; filename=" + FileName);
- Response.WriteFile(Server.MapPath("~/download/" + FileName));
- Response.Flush();
- Response.Close();
- Response.End();
- }
- #endregion
-
- #region " [ Get report Data ] "
- private DataTable getReportData()
- {
- DataSet dsData = new DataSet();
- SqlConnection sqlCon = null;
- SqlDataAdapter sqlCmd = null;
-
- try
- {
- using (sqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString))
- {
- sqlCmd = new SqlDataAdapter("USP_GETEmployeeDetails", sqlCon);
- sqlCmd.SelectCommand.CommandType = CommandType.StoredProcedure;
-
- sqlCon.Open();
- sqlCmd.Fill(dsData);
-
- sqlCon.Close();
- }
- }
- catch
- {
- throw;
- }
- return dsData.Tables[0];
- }
- #endregion
- }
- }
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:
- Microsoft.ReportViewer.Common.dll
- Microsoft.ReportViewer.ProcessingObjectModel.dll
- Microsoft.ReportViewer.WebForms.dll
- Microsoft.ReportViewer.WinForms.dll (not required for web application)
For more detailed code and database script information download the source code attached.