Applying ReportViewer Ver. 10 (RDLC) in Asp.Net and using Visual Web Developer 2010 Express Edition

Many organizations require to have Reporting that can be accessed online. There are many options on the web including SQL Server Reporting Service (SSRS) and Asp.Net ReportViewer Control.
If you install SSRS in server then the server becomes Report Server. SSRS makes easy on report development also deployment. After developer creates report he can deploy it to Report Server so users can view that report by browsing to Report Server Url.
The lack of SSRS is the SSRS service needs to be installed on top of SQL Server so that organization must manage their SQL Server and SSRS even if your main database is not using SQL Server. Cient validation is also often being issue since developer can not use javascript in Report Designer.

On the other hand, ReportViewer Control is more flexible. It is built on Asp.Net and Reporting by ReportViewer doesn’t need to have SQL Server if you don’t want to install it. Also client validation is not an issue anymore. Asp.Net has many techniques to do validation before Data Source is sent to ReportViewer.

This blog post will focus on applying ReportViewer Ver. 10. I use VS Web Developer 2010 Express and did some steps below before coding:

  1. Get ReportViewer Control Ver.10 from this link and install it
    That component will be registered on C:\Windows\assembly folder after install.
  2. Using xcopy command, copy lib file which is Microsoft.ReportViewer.WebForms.dll from folder C:\Windows\assembly\GAC_MSIL\Microsoft.ReportViewer.WebForms\ to your own folder
  3. Create New Asp.Net Empty Web App Project
  4. Add reference to copied Microsoft.ReportViewer.WebForms.dll previously
  5. Add ReportViewer Ver.10 Control to your toolbox list
  6. Open Web.Config file. Make sure ReportViewer Version is and add <httpHandlers> tag. Below is my web.config file:
    		<compilation debug="true" targetFramework="4.0">
    				<add assembly="Microsoft.ReportViewer.WebForms, Version=, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
    				<add assembly="Microsoft.ReportViewer.Common, Version=, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
    				<add assembly="Microsoft.Build.Framework, Version=, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
    				<add assembly="System.Management, Version=, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/></assemblies></compilation>
                <add verb="*" path="Reserved.ReportViewerWebControl.axd" type = "Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />

In order to use ReportViewer Control, just drag and drop the control to your web form but you also have to add Asp.Net Ajax ScriptManager Control.
So the page’s html codes will look like below:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebRdlc.aspx.cs" Inherits="UsingRdlc.WebRdlc" %>

<%@ Register Assembly="Microsoft.ReportViewer.WebForms, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
    Namespace="Microsoft.Reporting.WebForms" TagPrefix="rsweb" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        <rsweb:ReportViewer ID="ReportViewer1" runat="server" Width="650" Height="620">

ScriptManager control is necessary in order to ReportViewer can run.

In this example, I use SQL server 2008 R2 with sample Database, AdventureWorks. I use Ms Visual Studio BIDS to design a Report. This BIDS comes from SSRS installation.
I create a Report’s DataSet with its name is AdvWorksDataSource. The report simply views Human Resources Dept and after I created it then started to code.

I will show two ways using this ReportViewer. One is using Asp.Net tags or without C# coding to populate DataSource for ReportViewer and two is using C# code.

Option 1 – Full Asp.Net Tags

<form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        <rsweb:ReportViewer ID="ReportViewer1" runat="server" Width="650" Height="600">
        <LocalReport ReportPath="HumanResourcesDept.rdlc">
            <DataSources >
            <rsweb:ReportDataSource DataSourceId="sqlhrd" Name="AdvWorksDataSource" />
        <asp:SqlDataSource runat="server" ID="sqlhrd" 
         ConnectionString="Data Source=.\SQLDEV;Initial Catalog=AdventureWorks;Integrated Security=SSPI"
         SelectCommand="select * from HumanResources.Department">

The report, HumanResourcesDept.rdlc putted on same directory with this Web root path
Report’s DataSet, AdvWorksDataSource must be reflected on <rsweb:ReportViewer> tag as Name attribute.
DataSourceId is pointed to SqlDataSource.

Option 2 – C# Coding

The aspx file Form’s html:

<form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        <rsweb:ReportViewer ID="ReportViewer1" runat="server" Width="650" Height="620">

Not much elements on above html since I will do generate report via C# coding.

protected void Page_Load(object sender, EventArgs e)
		if (!Page.IsPostBack)
			string connectionstring = @"Data Source=.\SQLDEV;Initial Catalog=AdventureWorks;Integrated Security=SSPI";
			DataTable dt = new DataTable();
			SqlConnection dbconn = new SqlConnection(connectionstring);
			string query = "select * from HumanResources.Department";
			SqlCommand cmd = new SqlCommand(query, dbconn);
			cmd.CommandTimeout = 720;
			SqlDataAdapter da = new SqlDataAdapter(cmd);

			ReportViewer1.ProcessingMode = Microsoft.Reporting.WebForms.ProcessingMode.Local;
			LocalReport localReport = ReportViewer1.LocalReport;
			localReport.ReportPath = "HumanResourcesDept.rdlc";
			ReportDataSource datasource = new ReportDataSource("AdvWorksDataSource", dt);

Above code produce DataTable object and then attached on ReportDataSource programmatically.

Agung Gugiaji

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s