Вы находитесь на странице: 1из 11

How to create rdlc reports in asp.

net
In this Article I will explain how to create RDLC Reports in ASP.Net.
Finally my output looks like this.



I created a table with name Employee



Now I am creating an empty web application as shown



Right click on the solution add Report Wizard as shown below.


After adding report wizard, we have to configure Data source as shown, I am selecting my database.


Click Next->Next
Select the Database objects, I am selecting my table name and stored procedure name as shown ,Click
Finish.


It displays dataset properties click Next


Drag the available fields


Click Next->Next->Next->Finish.

Finally rdlc Design will display like this.


Now I am creating a web form with name Report.aspx, click Add


Now I am designing my UI as shown


Drag the report viewer and choose Report as our report name as shown


Drag the script manger on the UI Screen.
Finally my UI looks as shown.


We can find the Report.aspx code
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Report.aspx.cs" Inherits="RdlcE
xample.Report" %>

<%@ Register assembly="Microsoft.ReportViewer.WebForms, Version=10.0.0.0,
Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a" namespace="Microsoft.Reporting.WebForms" tagprefix="rswe
b" %>

<!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">
<div>
<div>
<label>Name</label>
<asp:DropDownList ID="ddlname" runat="server">
<asp:ListItem>All</asp:ListItem>
</asp:DropDownList>
<label>Designation</label>
<asp:DropDownList ID="ddldesignation" runat="server">
<asp:ListItem>All</asp:ListItem>
</asp:DropDownList>
<label>Address</label>
<asp:DropDownList ID="ddladdress" runat="server">
<asp:ListItem>All</asp:ListItem>
</asp:DropDownList>
<br />
<asp:Button ID="btnReport" runat="server" Text="GenerateReport"
onclick="btnReport_Click" />


</div>
</div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<rsweb:ReportViewer ID="ReportViewer1" runat="server" Font-Names="Verdana"
Font-Size="8pt" InteractiveDeviceInfos="(Collection)"
WaitMessageFont-Names="Verdana" WaitMessageFont-Size="14pt">
<LocalReport ReportPath="Report1.rdlc">
<DataSources>
<rsweb:ReportDataSource DataSourceId="ObjectDataSource1" Name="DataSet1"
/>
</DataSources>
</LocalReport>
</rsweb:ReportViewer>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
SelectMethod="GetData"
TypeName="RdlcExample.Report">
</asp:ObjectDataSource>
</form>
</body>

</html>










Name

All

Designation

All

Address

All

























Now I am implementing the Logic for generating report in Report.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.Data;
using System.Data.SqlClient;

namespace RdlcExample
{
public partial class Report : System.Web.UI.Page
{

static DataTable DatTab { get; set; }
//DataSet dt
public static string Name { get; set; }
public static bool flag { get; set; }
public static SqlConnection con = new SqlConnection("Data
Source=CHINNU;Initial Catalog=dotnetdb;User ID=sa;Password = password123;");
public static string Designation { get; set; }
public static string Address { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
loadDdlDesignation();
loadDdlAddress();
loadDdlName();
GetData();


}
}
public void loadDdlAddress()
{

try
{

if (con.State == ConnectionState.Closed)
{
con.Open();
}

SqlCommand com = new SqlCommand("select EmpAddress from
Employee", con);
SqlDataAdapter da = new SqlDataAdapter(com);
DataSet ds = new DataSet();
da.Fill(ds);

ddladdress.DataTextField =
ds.Tables[0].Columns["EmpAddress"].ToString();

ddladdress.DataSource = ds.Tables[0];
ddladdress.DataBind();
ddladdress.Items.Insert(0, "All");
}
catch (Exception)
{


}


}
public void loadDdlDesignation()
{

try
{

if (con.State == ConnectionState.Closed)
{
con.Open();
}

SqlCommand com = new SqlCommand("select EmpDesignation from
Employee", con); // table name
SqlDataAdapter da = new SqlDataAdapter(com);
DataSet ds = new DataSet();
da.Fill(ds); // fill dataset
ddldesignation.DataTextField =
ds.Tables[0].Columns["EmpDesignation"].ToString(); // text field name of
table dispalyed in dropdown

ddldesignation.DataSource = ds.Tables[0]; //assigning
datasource to the dropdownlist
ddldesignation.DataBind(); //binding dropdownlist
ddldesignation.Items.Insert(0, "All");
}
catch (Exception)
{


}


}



public void loadDdlName()
{

try
{
if (con.State == ConnectionState.Closed)
{
con.Open();
}


SqlCommand com = new SqlCommand("select EmpName from
Employee", con);
SqlDataAdapter da = new SqlDataAdapter(com);
DataSet ds = new DataSet();
da.Fill(ds);

ddlname.DataTextField =
ds.Tables[0].Columns["EmpName"].ToString();

ddlname.DataSource = ds.Tables[0];
ddlname.DataBind();
ddlname.Items.Insert(0, "All");
}
catch (Exception)
{


}


}
public static DataTable GetData()
{
try
{

DatTab = new DataTable();

if (!flag)
{
SqlCommand cmd = new SqlCommand("select * from
Employee", con);
//cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter adap = new SqlDataAdapter(cmd);
adap.Fill(DatTab);
con.Close();
}

else
{
if (con.State == ConnectionState.Closed)
{
con.Open();
}
using (SqlCommand cmd = new SqlCommand("Show_Report",
con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@EmpName",
SqlDbType.NVarChar)).Value = Convert.ToString(Name) == "All" ? "-1" :
Convert.ToString(Name);
cmd.Parameters.Add(new
SqlParameter("@EmpDesignation", SqlDbType.NVarChar)).Value =
Convert.ToString(Designation) == "All" ? "-1" :
Convert.ToString(Designation);
cmd.Parameters.Add(new SqlParameter("@EmpAddress",
SqlDbType.NVarChar)).Value = Convert.ToString(Address) == "All" ? "-1" :
Convert.ToString(Address);

cmd.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(DatTab);
con.Close();
}
}
con.Close();
}
catch (Exception ex)
{
con.Close();
}
return DatTab;
}

protected void btnReport_Click(object sender, EventArgs e)
{
flag = true;
Designation = ddldesignation.Text.Trim();
Address = ddladdress.Text.Trim();
Name = ddlname.Text.Trim();



GetData();

ReportViewer1.LocalReport.Refresh();
}
}
}
After completing all the steps Press F5 .

OutPut:

- See more at: http://www.dotnetsharepoint.com/2013/09/how-to-create-rdlc-reports-in-
aspnet.html#.U33EMvmSwsQ

Вам также может понравиться