RDLC (Report Definition Language Client-Side) is a powerful reporting tool provided by Microsoft for creating and displaying reports in .NET applications.
In this blog post, we'll explore how to create and use RDLC reports in an ASP.NET Web Forms (aspx) application with an Oracle Database. We'll cover everything from setting up the environment, designing reports, and displaying them in your application.
What is RDLC?
RDLC is a client-side reporting technology that allows you to create and display reports in .NET applications. It provides a flexible and feature-rich way to design reports using Visual Studio and display them using the ReportViewer control.
1. Setting Up the Environment
Before we start, ensure that you have the following tools and libraries installed:
- Visual Studio (2017 or later)
- Oracle Data Provider for .NET (ODP.NET)
- ReportViewer Control (included in Visual Studio)
Step 1: Install Oracle Data Provider for .NET (ODP.NET)
Download and install the Oracle Data Provider for .NET (ODP.NET) from the Oracle website.
Step 2: Add ReportViewer Control to Your Project
Add the ReportViewer control to your ASP.NET Web Forms project:
<%@ Register Assembly="Microsoft.ReportViewer.WebForms" Namespace="Microsoft.Reporting.WebForms" TagPrefix="rsweb" %>
2. Connecting to Oracle Database
To fetch data from an Oracle database, you need to establish a connection and retrieve data using ODP.NET.
Step 1: Create a Connection String
Add the Oracle connection string to your web.config file:
<connectionStrings>
<add name="OracleDB" connectionString="User Id=your_username;Password=your_password;Data Source=your_tnsname;" providerName="Oracle.DataAccess.Client" />
</connectionStrings>
Step 2: Fetch Data from Oracle Database
Create a method to fetch data from the Oracle database:
using Oracle.DataAccess.Client;
using System.Data;
public DataTable GetDataFromOracle()
{
DataTable dt = new DataTable();
string connectionString = ConfigurationManager.ConnectionStrings["OracleDB"].ConnectionString;
using (OracleConnection con = new OracleConnection(connectionString))
{
string query = "SELECT * FROM Employees"; // Replace with your query
using (OracleCommand cmd = new OracleCommand(query, con))
{
con.Open();
using (OracleDataAdapter da = new OracleDataAdapter(cmd))
{
da.Fill(dt);
}
}
}
return dt;
}
3. Designing the RDLC Report
Design the RDLC report using Visual Studio's Report Designer.
Step 1: Add a New RDLC Report
Right-click on your project, select Add > New Item, and choose Report (RDLC). Name it EmployeeReport.rdlc.
Step 2: Design the Report
Drag and drop fields from the Data Sources window onto the report designer. For example:
- Add a Table control to display data.
- Bind the table to the dataset fields (e.g.,
EmployeeID,Name,Age).
4. Displaying the RDLC Report in ASP.NET
Use the ReportViewer control to display the RDLC report in your ASP.NET Web Forms application.
Step 1: Add ReportViewer Control to Your Page
Add the ReportViewer control to your .aspx page:
<rsweb:ReportViewer ID="ReportViewer1" runat="server" Width="100%" Height="500px">
<LocalReport ReportPath="Reports/EmployeeReport.rdlc"></LocalReport>
</rsweb:ReportViewer>
Step 2: Bind Data to the Report
Bind the data fetched from the Oracle database to the RDLC report:
using Microsoft.Reporting.WebForms;
using System.Data;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindReport();
}
}
private void BindReport()
{
// Fetch data from Oracle
DataTable dt = GetDataFromOracle();
// Create a ReportDataSource
ReportDataSource rds = new ReportDataSource("EmployeeDataSet", dt);
// Bind the data to the ReportViewer
ReportViewer1.LocalReport.DataSources.Clear();
ReportViewer1.LocalReport.DataSources.Add(rds);
ReportViewer1.LocalReport.Refresh();
}
5. Advanced Features
RDLC reports support advanced features like parameters, grouping, sorting, and charts. Let's explore some of these features.
Example: Adding Parameters
Add parameters to filter the report data:
// Add parameters to the report
ReportParameter[] parameters = new ReportParameter[1];
parameters[0] = new ReportParameter("Department", "IT");
ReportViewer1.LocalReport.SetParameters(parameters);
ReportViewer1.LocalReport.Refresh();
Example: Adding Charts
Add a chart to visualize data:
- Drag and drop a Chart control onto the report designer.
- Bind the chart to the dataset fields (e.g.,
NameandAge).
6. Deploying the Application
When deploying your application, ensure that the ReportViewer control and RDLC reports are properly configured on the server.
Step 1: Install ReportViewer Runtime
Install the Microsoft Report Viewer Runtime on the server where the application is hosted.
Step 2: Configure Web.config
Add the following configuration to your web.config file:
<system.web>
<httpHandlers>
<add verb="*" path="Reserved.ReportViewerWebControl.axd" type="Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, Version=15.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" />
</httpHandlers>
</system.web>
Conclusion
RDLC reports are a powerful tool for creating and displaying reports in ASP.NET Web Forms applications. By connecting to an Oracle database, designing reports, and using the ReportViewer control, you can create dynamic and user-friendly reports. Experiment with the examples provided and explore more advanced features to enhance your reporting capabilities.