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 http://www.microsoft.com/en-us/download/details.aspx?id=6442 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\10.0.0.0__b03f5f7f11d50a3a 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 10.0.0.0 and add <httpHandlers> tag. Below is my web.config file:
    <configuration>
    	<system.web>
    		<compilation debug="true" targetFramework="4.0">
               
    			<assemblies>                
    				<add assembly="Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
    				<add assembly="Microsoft.ReportViewer.Common, Version=10.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
    				<add assembly="Microsoft.Build.Framework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
    				<add assembly="System.Management, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/></assemblies></compilation>
            <httpHandlers>
                <add verb="*" path="Reserved.ReportViewerWebControl.axd" type = "Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
            </httpHandlers>
    	</system.web>
        
    </configuration>
    


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=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
    Namespace="Microsoft.Reporting.WebForms" TagPrefix="rsweb" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <rsweb:ReportViewer ID="ReportViewer1" runat="server" Width="650" Height="620">
        
        </rsweb:ReportViewer>
    </div>
    </form>
</body>
</html>

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

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">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <rsweb:ReportViewer ID="ReportViewer1" runat="server" Width="650" Height="620">
        
        </rsweb:ReportViewer>
    </div>
    </form>

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);
			dbconn.Open();
			string query = "select * from HumanResources.Department";
			SqlCommand cmd = new SqlCommand(query, dbconn);
			cmd.CommandTimeout = 720;
			SqlDataAdapter da = new SqlDataAdapter(cmd);
			da.Fill(dt);
			da.Dispose();
			dbconn.Close();

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

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

Cheers,
Agung Gugiaji

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s