TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Working On RDLC Report In C#
Manoj Kalla
Dec 09, 2015
148.9k
0
6
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Print
Other Artcile
In this article you will learn about the working of RDLC Report in C#.
RDLCDemo.zip
Create Empty Web Site Project
Add New Web Form.
Add Connection String in WEB.CONFIG file.
<connectionStrings>
<add name=
"ConnectionString1"
connectionString=
"Data Source=SERVER NAME;Initial Catalog=DATABASE NAME;Integrated Security=True"
providerName=
"System.Data.SqlClient"
/>
</connectionStrings>
Insert REPORTVIEWER tool on
DEFAULT.ASPX
,
While you drag and drop ReportViewer on Default.aspx page, at the backend Visual Studio insert the following code into WEB.CONFIG File.
<compilation debug=
"false"
targetFramework=
"4.5"
>
<assemblies>
<add assembly=
"Microsoft.ReportViewer.WebForms, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845DCD8080CC91"
/>
<add assembly=
"Microsoft.ReportViewer.Common, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845DCD8080CC91"
/>
<add assembly=
"Microsoft.Build.Framework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"
/> </assemblies>
<buildProviders>
<add extension=
".rdlc"
type=
"Microsoft.Reporting.RdlBuildProvider, Microsoft.ReportViewer.WebForms, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"
/> </buildProviders>
</compilation>
Insert strongly typed DATASET, right click on,
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.
Double click on MemberDataSet.xsd file.
MemberDataset canvas will open. Right click on canvas.
Right click on canvas and select DataTable option.
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.
Now right click again on MemberDataTable select ADDCOLUMN option or simply press CTRL + L to add what you want.
Your DataTable should look like the following,
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.
<httpHandlers>
<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"
/>
</httpHandlers>
Right click on project or solution explorer and add REPORT that is RDLC extension file. Give the name
MemberReport.rdlc.
By default empty RDLC (report file) will look like the following,
You can see left hand side Report Data | ToolBox | Server Explorer.
ReportData
: To manage report dataset and fields.
ToolBox
Now click on
Report Data
and click on
New
and select
Datasets
option just below title of toolbox REPORT.
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.
Update this dialogue as in the details above,
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.
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.
After inserting columns your report canvas will display as above image.
To make report title BOLD select table header row and mark BOLD from toolbar.
For title report header select TEXTBOX from TOOLBOX and write header detail and marked bold, italic as you like.
My Report designing completed.
Now, we understand code behind file code to execute report on report viewer of aspx page.
Code Behind Code of
DEFAULT.ASPX.CS
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Configuration;
using
System.Data;
using
System.Data.SqlClient;
using
Microsoft.Reporting.WebForms;
public
partial
class
_Default: System.Web.UI.Page
{
protected
void
Page_Load(
object
sender, EventArgs e)
{
if
(!IsPostBack)
{
//set Processing Mode of Report as Local
ReportViewer1.ProcessingMode = ProcessingMode.Local;
//set path of the Local report
ReportViewer1.LocalReport.ReportPath = Server.MapPath(
"~/MemberReport.rdlc"
);
//creating object of DataSet dsMember and filling the DataSet using SQLDataAdapter
MemberDataSet dsMember =
new
MemberDataSet();
string
ConStr = ConfigurationManager.ConnectionStrings[
"ConnectionString1"
].ConnectionString;
SqlConnection con =
new
SqlConnection(ConStr);
con.Open();
SqlDataAdapter adapt =
new
SqlDataAdapter(
"select * from tblMembers"
, con);
adapt.Fill(dsemp,
"MemberDataTable"
);
con.Close();
//Providing DataSource for the Report
ReportDataSource rds =
new
ReportDataSource(
"ReportDataSet"
, dsMember.Tables[0]);
ReportViewer1.LocalReport.DataSources.Clear();
//Add ReportDataSource
ReportViewer1.LocalReport.DataSources.Add(rds);
}
}
}
Now switch to DEFAULT.ASPX and drag and drop SCRIPT MANAGER inside main tool box of AJAX EXTENSIONS.
Now run and see your report will display as in the following screenshot,
You can convert above report with the following format,
1. Excel
2. PDF
3. Word
Table Structure
USE [Member]
GO
/****** Object:
Table
[dbo].[TblMembers] Script
Date
: 12/08/2015 23:29:28 ******/
SET
ANSI_NULLS
ON
GO
SET
QUOTED_IDENTIFIER
ON
GO
SET
ANSI_PADDING
ON
GO
CREATE
TABLE
[dbo].[TblMembers](
[MemberID] [
bigint
] IDENTITY(1,1)
NOT
NULL
,
[
name
] [
varchar
](50)
NULL
,
[place] [
varchar
](50)
NULL
,
[mobile] [
varchar
](50)
NULL
,
[Salary] [
decimal
](18, 2)
NULL
,
CONSTRAINT
[PK_TblMembers2]
PRIMARY
KEY
CLUSTERED
(
[MemberID]
ASC
)
WITH
(PAD_INDEX =
OFF
, STATISTICS_NORECOMPUTE =
OFF
, IGNORE_DUP_KEY =
OFF
, ALLOW_ROW_LOCKS =
ON
, ALLOW_PAGE_LOCKS =
ON
)
ON
[
PRIMARY
]
)
ON
[
PRIMARY
]
Default.aspx
<%@ Page Language=
"C#"
AutoEventWireup=
"true"
CodeFile=
"Default.aspx.cs"
Inherits=
"_Default"
%>
<%@ Register Assembly=
"Microsoft.ReportViewer.WebForms, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"
Namespace=
"Microsoft.Reporting.WebForms"
TagPrefix=
"rsweb"
%>
<!DOCTYPE html>
<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=
"828px"
></rsweb:ReportViewer>
</div>
</form>
</body>
</html>
Default.aspx.cs
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Configuration;
using
System.Data;
using
System.Data.SqlClient;
using
Microsoft.Reporting.WebForms;
public
partial
class
_Default: System.Web.UI.Page
{
protected
void
Page_Load(
object
sender, EventArgs e)
{
if
(!IsPostBack)
{
//set Processing Mode of Report as Local
ReportViewer1.ProcessingMode = ProcessingMode.Local;
//set path of the Local report
ReportViewer1.LocalReport.ReportPath = Server.MapPath(
"~/MemberReport.rdlc"
);
//creating object of DataSet dsmember and filling the DataSet using SQLDataAdapter
MemberDataSet dsMember =
new
MemberDataSet();
string
ConStr = ConfigurationManager.ConnectionStrings[
"ConnectionString1"
].ConnectionString;
SqlConnection con =
new
SqlConnection(ConStr);
con.Open();
SqlDataAdapter adapt =
new
SqlDataAdapter(
"select * from tblMembers"
, con);
adapt.Fill(dsMember,
"MemberDataTable"
);
con.Close();
//Providing DataSource for the Report
ReportDataSource rds =
new
ReportDataSource(
"ReportDataSet"
, dsMember.Tables[0]);
ReportViewer1.LocalReport.DataSources.Clear();
//Add ReportDataSource
ReportViewer1.LocalReport.DataSources.Add(rds);
}
}
}
Please, feel free to ask me any doubt or questions.
ASP.NET
RDLC Report
ReportDataSet
REPORTVIEWER
Recommended Free Ebook
Printing in C# Made Easy
Download Now!
Similar Articles