12
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.

How to Create Rdlc Reports in ASP

Embed Size (px)

DESCRIPTION

create RDLC reports in asp.net

Citation preview

Page 1: How to Create Rdlc Reports in ASP

How to create rdlc reports in asp.netIn 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.

Page 2: How to Create Rdlc Reports in ASP

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

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

It displays dataset properties click Next

Page 3: How to Create Rdlc Reports in ASP

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

Page 4: How to Create Rdlc Reports in ASP

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.

Page 5: How to Create Rdlc Reports in ASP

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

<%@ 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">    <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" />

Page 6: How to Create Rdlc Reports in ASP

       </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

Page 7: How to Create Rdlc Reports in ASP

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(); }

Page 8: How to Create Rdlc Reports in ASP

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) {

}

}

Page 9: How to Create Rdlc Reports in ASP

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(); }

Page 10: How to Create Rdlc Reports in ASP

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: