Showing Reports by ReportViewer Control and NHibernateIn 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 :
Customers |
CustomerID int IDENTITY(1,1) NOT NULL, FirstName nvarchar(50) default NULL, LastName nvarchar(50) default NULL, Address nvarchar(150) default NULL, PRIMARY KEY (CustomerID) |
The Business Class is:
Customer.cs |
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:
Customer.hbm.xml
(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 :
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEia652itvp8Y5LScEFuLz5o0ObIahoX8L-7bNs3A2KKTfdyCMeZi7hXu4vWC6vDj70swG-Fvub-Z-7i48DEgLj2SAJ3sPz9IqRzO2GmlXCu3n1BugtPwLY-_ffehteyEabi6Ves6QFM7L4/s200/DataSet.JPG)
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").
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgwMzW-eP_Vx2PZ6BQZbyJynolTll4piOtkLH5HSwp2EIiA8U93prIW8zIEwB8FtlY0IRDPCHk94-tqNH6EoSLbz2ezNX767SgK4S6pmJ5l3v2ASEhDh3yAQ9UBAqFp-I2-0E5Q30yrXU4/s400/RDLC+Design.JPG)
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"; rvCustomer.LocalReport.DataSources.Clear();
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) { ds.Customers.AddCustomersRow(customer.Id.ToString(), customer.FirstName, customer.LastName, customer.Address); }
rvCustomer.LocalReport.DataSources.Add( new ReportDataSource("DataSet1_DataTable1", ds.Customers)); } // data Source name "DataSet1_DataTable1" generally made by DataSet name + "_" // + DataTable name of the DataSet } } |
Finally the Output :
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjCGxCHBq1VMT_T_aerT41DSnsDhXzjBt9BTmQIo7UmiNoaKxqprlC3eHNwbosuaBn-Op5R8xhlelhSttl-lmXSH4_gc2p07jq2vXxvq-xKlSvlnGPC83vcWSep3iydg4g6Os_QvDt1nZM/s400/Output.JPG)
----------------------------------------------------------