Friday, October 8, 2010

Working with Crystal Report and ASP.Net

How to create reports using Crystal Reports in ASP.Net?

Introduction
Integrating Crystal Reports with .Net can be a real challenge if you don't know number
of things required for it. I am going to explain you here those things with a very simple example.

Before explaining about implementing crystal report in ASP.Net, i assume you have the knowledge of following:
- ASP.Net 2.0
- C# or VB.Net [I will use C# for this tutorial, you can use VB.Net if you wish.]
- SQL Server 2005
- XML Schema

Now follow below simple steps to impletment crystal reports:
1) Create a Dataset DS which has all the data to be displayed in Report.
  • you can create dataset by fetching data from Database or by creating static dataset in your sample application.
2) Create an XML Schema definition.
  •   Add an XML Schema by Right Clicking your application -> Add New Item -> DataSet (.xsd File) and create all the tables in schema as per in the Dataset.
3) Add a crystal report viewer in your web form and call it crystalreportviewer1 for example.
4) Add a Crystal Report in your application and select "Using the Report Wizard" in the "Crystal Report Gallery" popup.

Crystal Reports Gallery


5) Now Standard Report Creation Wizard will be shown. 
Select Create New Connection -> ADO.Net -> Make New Connection(if displayed). It will display a Connection Popup.
   - Select  the XML Schema Definition we have created in the File Path and Finish.
   - Now it will display XSD name as a Connection and the Table names under it as per created in XSD. Select all the tables displayed under connection in the "Available DataSource" to the right side in "Selected tables".

Report Creation Wizard

- Now follow the wizard steps.
- select the fields to be displayed in the "Fields" Box. Click Next.
- if you need any grouping in your report, select the Group fields on which grouping will be applied.Click Next.
- if you need any Summary to be displayed in reports, select the summary fields.
- follow the report steps and finish.
6) Format your Crystal Report as per your requirement. You can add Group Section by Right Click in the Report -> Insert -> Group, if you have selected group fields in above steps.
7) Now Turn to your Web page in which you want to display report, where your code will be looked like below.

Declare 2 public variable for ReportDocument and Dataset of type created XSD.

ReportSchema dsschema = new ReportSchema();
ReportDocument rpt = new ReportDocument();

Now write down below code in your button click event where you want to display the report.

//Fetch data from the records dataset to the dataset object of type Schema
dsschema.Tables["Table1"].Load(ds.Tables["Table1"].CreateDataReader());
dsschema.Tables["Table2"].Load(ds.Tables["Table2"].CreateDataReader());

rpt.Load(Server.MapPath("Report.rpt"), OpenReportMethod.OpenReportByTempCopy);

CrystalReportViewer1.ReportSource = rpt;
//CrystalReportViewer1.RefreshReport();
CrystalReportViewer1.DataBind();

CrystalReportViewer1.DisplayGroupTree = false;
//Display the Toolbar in Crystal Report Viewer Control
CrystalReportViewer1.DisplayToolbar = true;
//Hide Business Objects Logo on Crystal Report Viewer Control
CrystalReportViewer1.HasCrystalLogo = false;

There you go...You are now ready to execute above script to display your crystal report.
if you have any query or concerns, then please write down a comment or mail me at shahbrijal@gmail.com

No comments: