Working On RDLC Report In C#

  1. Create Empty Web Site Project

    empty web

  2. Add New Web Form.

    web form

  3. Add Connection String in WEB.CONFIG file.
    1. <connectionStrings>  
    2.     <add name="ConnectionString1" connectionString="Data Source=SERVER NAME;Initial Catalog=DATABASE NAME;Integrated Security=True" providerName="System.Data.SqlClient" />   
    3. </connectionStrings>  
  4. Insert REPORTVIEWER tool on DEFAULT.ASPX,

    REPORTVIEWER

    While you drag and drop ReportViewer on Default.aspx page, at the backend Visual Studio insert the following code into WEB.CONFIG File.
    1. <compilation debug="false" targetFramework="4.5">  
    2.     <assemblies>  
    3.         <add assembly="Microsoft.ReportViewer.WebForms, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845DCD8080CC91" />  
    4.         <add assembly="Microsoft.ReportViewer.Common, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845DCD8080CC91" />  
    5.         <add assembly="Microsoft.Build.Framework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" /> </assemblies>  
    6.     <buildProviders>  
    7.         <add extension=".rdlc" type="Microsoft.Reporting.RdlBuildProvider, Microsoft.ReportViewer.WebForms, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" /> </buildProviders>  
    8. </compilation>  
  5. Insert strongly typed DATASET, right click on,

    DATASET

  6. As you insert above DATASET into project, visual studio will ask you confirmation about location of dataset that is APP_CODE. Select Yes for this dialogue box.

    APP_CODE: By default Visual Studio place all code and dataset related things inside this folder. Advantage of placing inside APP_CODE is that it will compile all things into one dll file called APP_CODE.DLL.

    app code
  7. Double click on MemberDataSet.xsd file.

    MemberDataSet

    MemberDataset canvas will open. Right click on canvas.

  8. Right click on canvas and select DataTable option.

    datatable

  9. Create strongly typed DATATABLE. By default datable will be created with named DATATABLE1, By right click or single click on Title DATATABLE you can Rename the DataTable with MemberDataTable.

  10. Now right click again on MemberDataTable select ADDCOLUMN option or simply press CTRL + L to add what you want.

    MemberDataTable

  11. Your DataTable should look like the following,

    DataTable

  12. Add the following NAMESPACE at top of the DEAFULT.ASPX FILE.

    using System.Configuration;

    Above namespace for fetching connection string from WEB.CONFIG file.

    using System.Data;

    Above namespace for creating or using DATASET things (ADO.NET).

    using System.Data.SqlClient;

    Above namespace for interacting with Microsoft SQL Server.

    using Microsoft.Reporting.WebForms;

    Above namespace for interacting with Report Data Source and Report component for web form. As you insert above namespace system will add the following code into web.config file.
    1. <httpHandlers>  
    2.     <add path="Reserved.ReportViewerWebControl.axd" verb="*" type="Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" validate="false" />  
    3. </httpHandlers>  
  13. Right click on project or solution explorer and add REPORT that is RDLC extension file. Give the name MemberReport.rdlc.

    MemberReport

  14. By default empty RDLC (report file) will look like the following,

    RDLC

  15. You can see left hand side Report Data | ToolBox | Server Explorer.

    ReportData : To manage report dataset and fields.

    reportdata 

    ToolBox

    ToolBox

  16. Now click on Report Data and click on New and select Datasets option just below title of toolbox REPORT.

    newreportdata 

  17. The following dialogue box will appear.

    Give Name as ReportDataSet and select DataSource as previously created STRONGLY TYPED DATASET called MemberDataSet from dropdownlist. Available Datasets will be your STRONGLY TYPED TABLE.

    Your table columns and datatype table will appear.

    datasetproperties

    Update this dialogue as in the details above,

    datasetproperties1

    Now switch to ToolBox menu and select Table tool and drag and drop on report file. After drag and drop Table, adjust table and report canvas area by stretching. On table you can create columns as per your requirement.

    To Insert field on report’s table there are two ways.

    insert field

    Second way is drag and drop field from REPORT DATA toolbox select DATASETS, DataSet, Select Field which you want on report.

    You can create extra columns by right click on table.

    right click

    right click

  18. After inserting columns your report canvas will display as above image.

  19. To make report title BOLD select table header row and mark BOLD from toolbar.

  20. For title report header select TEXTBOX from TOOLBOX and write header detail and marked bold, italic as you like.

  21. My Report designing completed.

    designing

  22. Now, we understand code behind file code to execute report on report viewer of aspx page.
    Code Behind Code of DEFAULT.ASPX.CS
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Web;  
    5. using System.Web.UI;  
    6. using System.Web.UI.WebControls;  
    7. using System.Configuration;  
    8. using System.Data;  
    9. using System.Data.SqlClient;  
    10. using Microsoft.Reporting.WebForms;  
    11. public partial class _Default: System.Web.UI.Page  
    12. {  
    13.     protected void Page_Load(object sender, EventArgs e)  
    14.     {  
    15.         if (!IsPostBack)  
    16.         {  
    17.             //set Processing Mode of Report as Local   
    18.             ReportViewer1.ProcessingMode = ProcessingMode.Local;  
    19.             //set path of the Local report   
    20.             ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/MemberReport.rdlc");  
    21.             //creating object of DataSet dsMember and filling the DataSet using SQLDataAdapter   
    22.             MemberDataSet dsMember = new MemberDataSet();  
    23.             string ConStr = ConfigurationManager.ConnectionStrings["ConnectionString1"].ConnectionString;  
    24.             SqlConnection con = new SqlConnection(ConStr);  
    25.             con.Open();  
    26.             SqlDataAdapter adapt = new SqlDataAdapter("select * from tblMembers", con);  
    27.             adapt.Fill(dsemp, "MemberDataTable");  
    28.             con.Close();  
    29.             //Providing DataSource for the Report   
    30.             ReportDataSource rds = new ReportDataSource("ReportDataSet", dsMember.Tables[0]);  
    31.             ReportViewer1.LocalReport.DataSources.Clear();  
    32.             //Add ReportDataSource   
    33.             ReportViewer1.LocalReport.DataSources.Add(rds);  
    34.         }  
    35.     }  
    36. }  
  23. Now switch to DEFAULT.ASPX and drag and drop SCRIPT MANAGER inside main tool box of AJAX EXTENSIONS.

  24. Now run and see your report will display as in the following screenshot,

    learn

    learn

    You can convert above report with the following format,

    1. Excel
    2. PDF
    3. Word

  25. Table Structure
    1. USE [Member]  
    2. GO  
    3. /****** Object: Table [dbo].[TblMembers] Script Date: 12/08/2015 23:29:28 ******/  
    4. SET ANSI_NULLS ON  
    5. GO  
    6. SET QUOTED_IDENTIFIER ON  
    7. GO  
    8. SET ANSI_PADDING ON  
    9. GO  
    10. CREATE TABLE [dbo].[TblMembers](  
    11. [MemberID] [bigint] IDENTITY(1,1) NOT NULL,  
    12. [name] [varchar](50) NULL,  
    13. [place] [varchar](50) NULL,  
    14. [mobile] [varchar](50) NULL,  
    15. [Salary] [decimal](18, 2) NULL,  
    16. CONSTRAINT [PK_TblMembers2] PRIMARY KEY CLUSTERED   
    17. (  
    18. [MemberID] ASC  
    19. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ONON [PRIMARY]  
    20. ON [PRIMARY]  
  26. Default.aspx
    1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
    2.     <%@ Register Assembly="Microsoft.ReportViewer.WebForms, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" Namespace="Microsoft.Reporting.WebForms" TagPrefix="rsweb" %>  
    3.         <!DOCTYPE html>  
    4.         <html xmlns="http://www.w3.org/1999/xhtml">  
    5.   
    6.         <head runat="server">  
    7.             <title></title>  
    8.         </head>  
    9.   
    10.         <body>  
    11.             <form id="form1" runat="server">  
    12.                 <div>  
    13.                     <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>  
    14.                     <rsweb:ReportViewer ID="ReportViewer1" runat="server" Width="828px"></rsweb:ReportViewer>  
    15.                 </div>  
    16.             </form>  
    17.         </body>  
    18.   
    19.         </html>  
  27. Default.aspx.cs
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Web;  
    5. using System.Web.UI;  
    6. using System.Web.UI.WebControls;  
    7. using System.Configuration;  
    8. using System.Data;  
    9. using System.Data.SqlClient;  
    10. using Microsoft.Reporting.WebForms;  
    11. public partial class _Default: System.Web.UI.Page  
    12. {  
    13.     protected void Page_Load(object sender, EventArgs e)  
    14.     {  
    15.         if (!IsPostBack)  
    16.         {  
    17.             //set Processing Mode of Report as Local   
    18.             ReportViewer1.ProcessingMode = ProcessingMode.Local;  
    19.             //set path of the Local report   
    20.             ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/MemberReport.rdlc");  
    21.             //creating object of DataSet dsmember and filling the DataSet using SQLDataAdapter   
    22.             MemberDataSet dsMember = new MemberDataSet();  
    23.             string ConStr = ConfigurationManager.ConnectionStrings["ConnectionString1"].ConnectionString;  
    24.             SqlConnection con = new SqlConnection(ConStr);  
    25.             con.Open();  
    26.             SqlDataAdapter adapt = new SqlDataAdapter("select * from tblMembers", con);  
    27.             adapt.Fill(dsMember, "MemberDataTable");  
    28.             con.Close();  
    29.             //Providing DataSource for the Report   
    30.             ReportDataSource rds = new ReportDataSource("ReportDataSet", dsMember.Tables[0]);  
    31.             ReportViewer1.LocalReport.DataSources.Clear();  
    32.             //Add ReportDataSource   
    33.             ReportViewer1.LocalReport.DataSources.Add(rds);  
    34.         }  
    35.     }  
    36. }  
    Please, feel free to ask me any doubt or questions.


Similar Articles