Following steps demonstrate how to create your first RDLC Report. This blog is divided into 2 parts:-
- Prepare the report from a single table and to solve the Report Viewer Configuration Error, if encountered.
- Prepare a single report from two different tables (Not-Related).
The database used here is Microsoft’s Northwind database.
Prepare a Report from a Single Table
# Add DataSet.
# Add DataTable
# Add Columns
# Add RDLC Report
# Choose DataSet
# Choose Fields
# Choose Layout
# Choose Style
# Finish the Wizard. The report will be then displayed
# Add ReportViewer to the ASP.NET page
# Following is the designer code snippet after adding the ReportViewer control
<%@ Register Assembly="Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" Namespace="Microsoft.Reporting.WebForms" TagPrefix="rsweb" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<rsweb:ReportViewer ID="ReportViewer1" runat="server" Width="600">
</rsweb:ReportViewer>
</form>
</body>
</html>
# Modifying the Code Behind section
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using Microsoft.Reporting.WebForms;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ReportViewer1.ProcessingMode = ProcessingMode.Local;
ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Report.rdlc");
Customers dsCustomers = GetData("select top 20 * from customers");
ReportDataSource datasource = new ReportDataSource("Customers", dsCustomers.Tables[0]);
ReportViewer1.LocalReport.DataSources.Clear();
ReportViewer1.LocalReport.DataSources.Add(datasource);
}
}
private Customers GetData(string query)
{
string conString = ConfigurationManager.ConnectionStrings["NorthWindConnectionString"]
.ConnectionString;
SqlCommand cmd = new SqlCommand(query);
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (Customers dsCustomers = new Customers())
{
sda.Fill(dsCustomers, "DataTable1");
return dsCustomers;
}
}
}
}
# Next Compile the code. There’s a chance that you might encounter the following error
and then when you click Continue, you’ll be finally redirected to your page which will be displaying the following error message.
The error is pretty straight-forward. Just as the message suggests add the following code to the Web.Config file
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<handlers>
<add name="ReportViewerWebControlHandler" preCondition="integratedMode" verb="*" path="Reserved.ReportViewerWebControl.axd" type="Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" />
</handlers>
</system.webServer>
Plz make sure that your PublicKeyToken is same as that of the Microsoft.ReportViewer.WebForms in the assemblies section of system.web.
# Next again run the code. You’ll be able to see the following result
You can see that field, Country, has appeared twice. This is because of the fact that I had done a GroupBy this field.
That’s it from this blog. Next as a continuation of it, I’ll be demonstrating how to display multiple tables(2) in a single report. You can find the link of that blog very shortly.
Thanks…