Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Using RDLC Report in .NET 4.0

0.00/5 (No votes)
19 Nov 2012 1  
Using RDLC report in .NET 4.0 version in ASP.NET and C#.

Introduction 

Using RDLC report in .NET 4.0 is quite different in comparison to prior versions of .NET. Since RDLC reports have their own importance, here are some easy steps to use them.

Using the code 

Please follow the below steps:

  1. Take .Xsd file (typed dataset) and create a table in this Typed dataset. This file must be in the App_Code folder.
  2. Build your Project/website.
  3. Now take .aspx Page and add reportviewer control in this file and choose RDLC report from reportviewer control.
  4. In RDLC wizard select Dataset (.xsd file added by you).
  5. Add All Column of Dataset (of .xsd) in required postion in RDLC file(you can tool table/text boxes from RDLC toolbox).
  6. Now Fill .xsd DataSet in code behind file.(please take care of this. see DataSet using in c#. I took same table name what is in my typed DataSet).
  7. Use Script Manager (in aspx file) as this is manadatory.
  8. Also use !IsPostBack in Page load to avoid infinte loop.
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 Microsoft.Reporting.WebForms;


public partial class Report_RDLC_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!IsPostBack)
            {
                DataSet DsTest = new DataSet("dtTest");
                DataTable dtTest = new DataTable("dtTest");
           
                dtTest.Columns.Add("EmpName", System.Type.GetType("System.String"));
                dtTest.Columns.Add("EmpId", System.Type.GetType("System.Int32"));
                dtTest.Columns.Add("EmpSal", System.Type.GetType("System.Double"));


                DataRow dr;
                for (int iIndex = 0; iIndex < 99; iIndex++)
                {
                    dr = dtTest.NewRow();
                    dr["EmpId"] = "100" + iIndex.ToString();
                    dr["EmpName"] ="Kapil Dev-  "+  iIndex.ToString();
                    dr["EmpSal"] = (iIndex * 32678).ToString();
                    dtTest.Rows.Add(dr);
                }
                DsTest.Tables.Add(dtTest);


                ReportViewer1.Visible = true;
                ReportDataSource datasource = new ReportDataSource("DataSet1", DsTest.Tables[0]);
                ReportViewer1.LocalReport.DataSources.Clear();
                ReportViewer1.LocalReport.DataSources.Add(datasource);
                ReportViewer1.LocalReport.Refresh();      
            }
        }
        catch
        {

        }
    }
}
<%@ Page Language="C#" AutoEventWireup="true" 
  CodeFile="Default.aspx.cs" Inherits="Report_RDLC_Default" %>

<%@ 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>
    <style type="text/css">
        .style1
        {
            width: 100%;
            height: 34px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server" >
        </asp:ScriptManager>
   <div style="text-align:center;width:80%;">
        
        
        <table style="width:60%;text-align:center;margin:0px 0px 0px 200px;">
        <tr>
        <td style="text-align:center" >
           <rsweb:ReportViewer ID="ReportViewer1" runat="server" Font-Names="Verdana" 
            Font-Size="8pt" InteractiveDeviceInfos="(Collection)" 
            WaitMessageFont-Names="Verdana" WaitMessageFont-Size="14pt" 
                BackColor="#CCCCFF" Height="600px" Width="100%">
            <LocalReport ReportPath="Report_RDLC\Report1.rdlc">
            </LocalReport>
        </rsweb:ReportViewer>
        
        </td>
        </tr>
        </table>    
    </div>
    </form>
</body>
</html>

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here