In this article I will explain with an example, how to filter RDLC Report in ASP.Net using C# and VB.Net.
RDLC Report data will be filtered on selection of DropDownList in ASP.Net.
Database
Here I am making use of Microsoft’s Northwind Database. You can download it from here.
HTML Markup
The HTML Markup consists of a RDLC ReportViewer control, a DropDownList to filter RDLC Report.
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
Country:
<asp:DropDownList ID="ddlCountries" runat="server" OnSelectedIndexChanged="CountryChanged"
AutoPostBack="true">
<asp:ListItem Text="All" Value="" />
<asp:ListItem Text="Spain" Value="Spain" />
<asp:ListItem Text="Germany" Value="Germany" />
<asp:ListItem Text="France" Value="France" />
<asp:ListItem Text="Canada" Value="Canada" />
</asp:DropDownList>
<hr />
<rsweb:ReportViewer ID="ReportViewer1" runat="server" Width="500" SizeToReportContent="true">
</rsweb:ReportViewer>
Namespaces
You will need to import the following namespaces.
C#
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using Microsoft.Reporting.WebForms;
VB.Net
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
Imports Microsoft.Reporting.WebForms
Designing and populating the RDLC Report from Database
Inside the Page Load event, the RDLC Report is populated from database using the BindReport function.
Inside the BindReport function, the selected value of the DropDownList i.e. the selected Country is passed to the GetData function where the value is passed as parameter and the records are filtered.
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindReport();
}
}
private void BindReport()
{
ReportViewer1.ProcessingMode = ProcessingMode.Local;
ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Report.rdlc");
Customers dsCustomers = GetData(ddlCountries.SelectedItem.Value);
ReportDataSource datasource = new ReportDataSource("Customers", dsCustomers.Tables[0]);
ReportViewer1.LocalReport.DataSources.Clear();
ReportViewer1.LocalReport.DataSources.Add(datasource);
}
private Customers GetData(string country)
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlCommand cmd = new SqlCommand("SELECT TOP 10 * FROM Customers WHERE Country = @Country OR @Country = ''"))
{
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
cmd.Parameters.AddWithValue("@Country", country);
using (Customers dsCustomers = new Customers())
{
sda.Fill(dsCustomers, "DataTable1");
return dsCustomers;
}
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindReport()
End If
End Sub
Private Sub BindReport()
ReportViewer1.ProcessingMode = ProcessingMode.Local
ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Report.rdlc")
Dim dsCustomers As Customers = GetData(ddlCountries.SelectedItem.Value)
Dim datasource As ReportDataSource = New ReportDataSource("Customers", dsCustomers.Tables(0))
ReportViewer1.LocalReport.DataSources.Clear()
ReportViewer1.LocalReport.DataSources.Add(datasource)
End Sub
Private Function GetData(ByVal country As String) As Customers
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using cmd As SqlCommand = New SqlCommand("SELECT TOP 10 * FROM Customers WHERE Country = @Country OR @Country = ''")
Using con As SqlConnection = New SqlConnection(conString)
Using sda As SqlDataAdapter = New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
cmd.Parameters.AddWithValue("@Country", country)
Using dsCustomers As Customers = New Customers()
sda.Fill(dsCustomers, "DataTable1")
Return dsCustomers
End Using
End Using
End Using
End Using
End Function
Filter RDLC Report using DropDownList in ASP.Net using C# and VB.Net
When a Country is selected in the DropDownList, the BindReport function is again called.
C#
protected void CountryChanged(object sender, EventArgs e)
{
this.BindReport();
}
VB.Net
Protected Sub CountryChanged(ByVal sender As Object, ByVal e As EventArgs)
Me.BindReport()
End Sub
Screenshot
Demo
Downloads