Friday, July 17, 2015

5 simple steps to Create RDLC Report in asp.net

In this article I am going to explain the 5 simple steps to Create RDLC Report in asp.net

Description:
In the previous article I have explained Implement Google like Custom Pagination in Datalist Control in Asp.net and Highlight The Current (selected) Page Number in Repeater Pagination.

Implementation:
I have created a table Student_Detail and having data. I want to display the detail of Students in Report. To create a report using RDLC follow the below given steps.

Step 1: Add Dataset to project/website.
Go to Add new item and add the Dataset to project. In this example DsStudentDetail .xsd is the name of dataset.  
5 simple steps to Create RDLC Report in asp.net

When you click on Add button you get a popup to place the Dataset file in App_Code folder. Click on Yes.



Step 2: Add Datatable to added Dataset (DsStudentDetail .xsd).
Right click on Dataset and add a DataTable to it.



Step 3: Add column/columns to DataTable
Right click on DataTable go to Add >> Column. Give a name to column’s but keep in mind columns names must match the name of columns of table. By default DataType of all columns are String if you want change the datatype of any column go to properties of that particular column.


Step 4: Add RDLC Report to website/project
Now go to Add new item and add the RDLC report to website. In this tutorial I am going to add the Reports (RDLC) Wizard.

Report wizard will open. Select the Datasource as Dataset (DsStudentDetail .xsd).  After that Click on Next button.



Now arrange the fields on this step. Drag and drop the required fields from available fields to values section. Hit the next button.



Click on next button if you don’t want to show the grand total and subtotal.



Choose the style in this step and click on Finish button.

Report.rdlc is added to the website. 


Step 5: Add webform
Add webform to website. Drag and drop the ReportViewer control from ToolBox (Reporting) and ScriptManager to webform.  ReportViewer control requires ScriptManager.

Complete HTML markup of webform:
<%@ Register Assembly="Microsoft.ReportViewer.WebForms, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" Namespace="Microsoft.Reporting.WebForms" TagPrefix="rsweb" %>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
        <rsweb:ReportViewer ID="ReportViewer1" runat="server"></rsweb:ReportViewer>
    </div>
    </form>
</body>
</html>
Add the namespace to code file
C#:
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Microsoft.Reporting.WebForms;
VB:
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports Microsoft.Reporting.WebForms

Write the code to Get the data from database and bind to Reportview.
C#:
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ToString());
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            BindReport();
        }
    }
    private void BindReport()
    {
        try
        {
            ReportViewer1.ProcessingMode = ProcessingMode.Local;
            //report path
            ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Report.rdlc");
            //
            SqlDataAdapter adp = new SqlDataAdapter("Select * from Student_Detail", con);
            //object of Dataset DsstudentDetail
            DsStudentDetail ds = new DsStudentDetail();
            adp.Fill(ds, "DataTable1");
            //Datasource for report
            ReportDataSource datasource = new ReportDataSource("DataSet1", ds.Tables[0]);
            ReportViewer1.Width = 600;
            ReportViewer1.LocalReport.DataSources.Clear();
            ReportViewer1.LocalReport.DataSources.Add(datasource);
        }
        catch(Exception ex)
        {
        }
    }

VB: 
    Private con As New SqlConnection(ConfigurationManager.ConnectionStrings("connection").ToString())
    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Not IsPostBack Then
            BindReport()
        End If
    End Sub
    Private Sub BindReport()
        Try
            ReportViewer1.ProcessingMode = ProcessingMode.Local
            'report path
            ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Report.rdlc")
            '
            Dim adp As New SqlDataAdapter("Select * from Student_Detail", con)
            'object of Dataset DsstudentDetail
            Dim ds As New DsStudentDetail()
            adp.Fill(ds, "DataTable1")
            'Datasource for report
            Dim datasource As New ReportDataSource("DataSet1", ds.Tables(0))
            ReportViewer1.Width = 600
            ReportViewer1.LocalReport.DataSources.Clear()
            ReportViewer1.LocalReport.DataSources.Add(datasource)
        Catch ex As Exception
        End Try
    End Sub


All is done. Now build the project and run.  
Result:
5 simple steps to Create RDLC Report in asp.net

  In this article we have learn 5 simple steps to create RDLC report in Asp.net(C#, VB). I hope you enjoyed this article. 

1 comment:

  1. this article will have result, because every thing has done at the end don't wast your time search for new

    ReplyDelete