Using ReportViewer RDLC for PDF, Word, etc. Reporting in ASP.NET

Today I shall recount on what I learnt when designing reporting technologies for my restaurant website developed in ASP.NET. ReportViewer is a cool tool used to make custom-display and custom-format printable reports extracted from data queries. A simple example of report is as below:

The image shows the rdlc report in light color and the GridView report in bold darker color, both embedded in a master page with navigation links to the left side. You can see the RDLC report feels and helps better.

Such a report can be printed, or saved as PDF, Word, Excel or any format supported by http://www.reportviewer.com

The steps used to create such a custom-report in your web application are:

1. Download ReportViewer.exe Redistributable Package . One of the later versions is here here http://www.microsoft.com/download/en/details.aspx?id=6442 .

2. Extract/Unzip the ReportViewer.exe installer. It will have a zipped and an installer package with the same name something like reportviewer_redist2010core inside the extracted folder. Extract that zipped core folder.

3. It will show up the dll files required to add assemblies to your web application. I have the following list of dlls in the extracted core folder:

FL_Microsoft_ReportViewer_Common_dll_117718_117718_x86_ln

FL_Microsoft_ReportViewer_DataVisualization_dll_117718_117718_x86_ln

FL_Microsoft_ReportViewer_ProcessingObject_125592_125592_x86_ln

FL_Microsoft_ReportViewer_WebForms_dll_117720_117720_x86_ln

FL_Microsoft_ReportViewer_WinForms_dll_117722_117722_x86_ln

4. Rename the ones you require. I needed all except the last one, since my report was used in a Web Form, and not as Windows Form. After renaming, the file names became:

Microsoft.ReportViewer.Common.dll

Microsoft.ReportViewer.DataVisualization.dll

Microsoft.ReportViewer.ProcessingObjectModel.dll

Microsoft.ReportViewer.WebForms.dll

Microsoft.ReportViewer.WinForms.dll

5. Copy and paste the dll files in the bin folder of your web application

6. Add reference to the included dll files in bin folder. You may add reference from the solution explorer on the right. If you are uploading dll files online, you do not have the solution explorer of Visual Studio. The best to do then is to add these lines in Web.config file:

<!--Add assembly lines within the assemblies tag -->
<add assembly="Microsoft.ReportViewer.Common, Version=10.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
<add assembly="Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>

<!--below the assembly tag add <buildBroviders> tag if not already present and add extension as below -->

<buildProviders>

<add extension=".rdlc" type="Microsoft.Reporting.RdlBuildProvider, Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
</buildProviders>

7. In the YourReport.ASPX web page where you embed the report, mention these reference lines to register the asssemblies if not already registered:

<%@ Import Namespace="Microsoft.Reporting.WebForms" %>

<%@ Register assembly="Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" namespace="Microsoft.Reporting.WebForms" tagprefix="rsweb" %>

8. Add a script manager and drag drop a report into the page. Your code inside the YourReport.aspx web page may look something like this:

    <rsweb:ReportViewer ID="ReportViewer1" runat="server" Font-Names="Verdana"
            Font-Size="8pt" InteractiveDeviceInfos="(Collection)"
            WaitMessageFont-Names="Verdana" WaitMessageFont-Size="14pt" Width="842px">

9. NOTE: If you do not find a Reporting Panel with a ReportViewer Control in the toolbox, you have to install the ReportViewer.exe in your Visual Studio IDE. Also install AJAX extensions for ScriptManager Control if not already installed.
10. Configure your DataSource that feeds up the rdlc report embedded in YourReport.aspx . The following code may help:

OnLoad is the event I added myself so that whenever the datasource loads, the event function loaddata defined in the same page is called:

    public void loaddata(object sender, EventArgs args)
    {
        SqlDataSource1.SelectCommand = thisOrderQuery();
        SqlDataSource1.SelectCommandType = SqlDataSourceCommandType.Text;
        SqlDataSource1.DataBind();    }
The function thisOrderQuery() returns a string representing simple SQL query command
11. NOTE: The SQL query should use the same column and table names as in the rdlc report format.
12. Design the RDLC report. You can have a header, and a body. I have used both. The image near the top shows the rdlc report in light color and the GridView report in bold darker color. You can see the RDLC report feels and helps better.
13. Make sure that in the RDLC report, for each data column to be filled, the expression on the backside is as below:
=Fields!YourColumnName.Value
For example, as I had to display the column [Order].Oid the expression behind the Order Id field in the RDLC report mentions :
=Fields!Oid.Value
Build the web application. It makes all the application into a single dll. But still make sure you upload the necessary dll files for running the reportviewer RDLC reports.
Best of Luck!
You may also visit my site and see how my report works. Since my website account is free, session time-outs sooner than necessary. Anyway, you may check
http://www.millenium.somee.com username 1 password abc

Leave a comment