Monday, May 5, 2008

Showing Reports by ReportViewer Control and NHibernate

Showing Reports by ReportViewer Control and NHibernate

In one of my Projects I was told to show some data using ReportViewer control and NHibernate. I had no idea how to do it.

Started Googling.... But I was quite surprised there was no such postings, even not found any information whether it is possible or not ! Never mind, started doing it and after completion found it pretty simple. Would like to share it with you.

Step 1:

I am using a single table, say Customers with the following schema :


CustomerID int IDENTITY(1,1) NOT NULL,

FirstName nvarchar(50) default NULL,

LastName nvarchar(50) default NULL,

Address nvarchar(150) default NULL,


The Business Class is:


namespace NHibernate.Example


public class Customer


public Customer()



private int id;

private string firstName;

private string lastName;

private string address;

public virtual int Id


get { return id; }

set { id = value; }


public virtual string FirstName


get { return firstName; }

set { firstName = value; }


public virtual string LastName


get { return lastName; }

set { lastName = value; }


public virtual string Address


get { return address; }

set { address = value; }




And Here is the Mapping file:

(Click to view)

Step 2:

We have to create a Typed Dataset in App_Code folder of our Website (or you can have your separate Project Library for DataSet). The data table of this DataSet will contain the fields which we want to show in our Report. Here, I would like to show all the fields of my Customers table. Therefore my Dataset looks like this :

Step 3 :

Create a RDLC file. Arrange the fields just by dragging and dropping the fileds of Dataset which you will find "Website data Sources" toolbar (to view this toolbar just click the RDLC body, go to the menu "Data" --> "Show Data Sources").

Step 4:

Now the whole environment is ready for coding. Just add a ReportViewer to an aspx page and use the following code and you are done to throw some data on your screen!

using System;

using System.Collections;

using Microsoft.Reporting.WebForms;

using NHibernate;

using NHibernate.Example;

public partial class CustomerReport : System.Web.UI.Page


DataSet1 ds = new DataSet1();

NHibernate.Cfg.Configuration newConfig = new NHibernate.Cfg.Configuration();

ISessionFactory sFactory;

protected void Page_Load(object sender, EventArgs e)


if (!Page.IsPostBack)


newConfig = newConfig.AddAssembly("NHibernate.Example");

sFactory = newConfig.BuildSessionFactory();

rvCustomer.LocalReport.ReportPath = "TestReport.rdlc";

rvCustomer.LocalReport.ReportEmbeddedResource = "ReportViewer.TestReport.rdlc";


ISession session = sFactory.OpenSession();

//get the list of customers using NHibernate

IList customers = session.CreateCriteria(typeof(Customer)).List();

//populate the typed Dataset

foreach (Customer customer in customers)








new ReportDataSource("DataSet1_DataTable1", ds.Customers));


// data Source name "DataSet1_DataTable1" generally made by DataSet name + "_"

// + DataTable name of the DataSet



Finally the Output :