Hi, Here i will explain how to create the RDLC reports using ReportViewer by getting data from Database. I have created a TextBox and a Button control by entering a ID in the Textbox, on Button Click, the details of the students are Shown in the ReportViewer with RDLC Reports.
Step 1 : Create the Database,Table for Which you are going to insert
Step
2 : Create New project
Step 3 : Create New Page and Add ScriptManager and ReportViewer to the created
page, as images Shown below.
After Adding ScriptManager and ReportViewer your page looks as Below.
Step
4 : Create a Dataset file as below in your project.
Step 5: Create the DataAdapter by Right Click in the Dataset File as below.
Complete the DB Connection wizard as below :
Your Dataset File looks as below. You can create one or More number of Adapters for a Dataset File
Step 6 :
Create a RDLC File in your Solution as Below.
Then
go to Menu of visual studio View > ReportData.
Here
you need to create a new Dataset as below
Three Steps to follow from here.
1.
Give the name of the Dataset
you have created.
2.
Select the Datasource name as
the file name of DataSet file which you previously created.
3.
Select the appropriate Dataset
listed in Available Datasets.
Step 7 : Now lets start design the RDLC File as shown below.
Here
I will show report by entering ID of the user which is in the Database.
As your
Aspx page Should looks Like below.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Page1.aspx.cs" Inherits="RDLCReport.Page1" %>
<%@ Register Assembly="Microsoft.ReportViewer.WebForms, Version=10.0.0.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
Namespace="Microsoft.Reporting.WebForms" TagPrefix="rsweb"
%>
<!DOCTYPE html
>
<html>
<head runat="server">
<title>Fourthbottle.com</title>
</head>
<body>
<form id="form1" runat="server">
<asp:TextBox ID="txtID" runat="server"></asp:TextBox>
<asp:Button ID="btn1" runat="server" Text="Generate report"
onclick="btn1_Click" />
<br />
<br />
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<rsweb:ReportViewer ID="ReportViewer1"
runat="server"
Visible="false">
<LocalReport ReportPath="Report1.rdlc"></LocalReport>
</rsweb:ReportViewer>
</div>
</form>
</body>
</html>
Note: <LocalReport ReportPath="Report1.rdlc"></LocalReport> should
be given inside your report viewer, Give your RDLC page name as ReportPath
where I have given as Report1.rdlc
Your Code Behind ( C# ) :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Reporting;
using Microsoft.ReportingServices;
using Microsoft.Reporting.WebForms;
using System.Configuration;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace RDLCReport
{
public
partial class Page1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs
e)
{
}
protected void btn1_Click(object sender, EventArgs
e)
{
ReportViewer1.Visible = true;
int id =Convert.ToInt32(txtID.Text);
ReportViewer1.ProcessingMode = ProcessingMode.Local;
ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Report1.rdlc");
DataSet ds = new DataSet();
ds = GetData(id);
if (ds.Tables[0].Rows.Count > 0)
{
ReportDataSource rds = new
ReportDataSource("DatasetName",
ds.Tables[0]);
ReportViewer1.LocalReport.DataSources.Clear();
ReportViewer1.LocalReport.DataSources.Add(rds);
}
}
private DataSet GetData(int id)
{
SqlConnection con = new
SqlConnection("Your
Connection String");
con.Open();
SqlCommand cmd = new
SqlCommand("select
* from Table1 where ID="+id+"");
using (con)
{
using (SqlDataAdapter
da = new SqlDataAdapter())
{
cmd.Connection
= con;
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds);
return (ds);
}
}
}
}
}
Add if the below lines of code is not available in your web-config File :
<system.web>
<httpHandlers>
<add path="Reserved.ReportViewerWebControl.axd" verb="*" type="Microsoft.Reporting.WebForms.HttpHandler,
Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
validate="false" />
</httpHandlers>
<compilation debug="true" targetFramework="4.0">
<assemblies>
<add assembly="Microsoft.ReportViewer.WebForms, Version=10.0.0.0,
Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />
<add assembly="Microsoft.ReportViewer.Common, Version=10.0.0.0,
Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />
<add assembly="Microsoft.Build.Framework, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=B03F5F7F11D50A3A" />
<add assembly="System.Management, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=B03F5F7F11D50A3A" />
</assemblies>
<buildProviders>
<add extension=".rdlc" type="Microsoft.Reporting.RdlBuildProvider,
Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a" />
</buildProviders>
</compilation>
. . . . . . . . .
. . . . . . . . . .
. . . . . . . . .
</system.web>
Run Your Application, Enter the ID And Submit ,you will get the details to your report.