How to Create Report using Crystal Report in ASP.NET

No.of Views1632
Bookmarked2 times
Downloads 
Votes3
By  youngmurukan   On  15 Feb 2010 21:02:15
Tag : Crystal Reports , General
In this article, i will show How to Create Report using Crystal Report in ASP.NET.Crystal report is most popular for create report in windows, web and other type of projects.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

In this article, I will show How to Create Report using Crystal Report in ASP.NET. Crystal Report is most popular for create report in windows, web and other type of projects.Therefore ASP.NET also has support to create reports using Crystal Report.

Prerequisites

  • SQL Server 2005/2008
  • Visual studio 2005/2008/2010
  • Crystal report(any versions)

Design Database

Let’s create simple database with one table for this demonstration. The database structure will be like followings, 

Image Loading

The database name is called HouseRental with HouseOwnerInfo table. There are eight columns inside the HouseOwnerInfo.

Implementation

Now I’m going to use this table and design report for present summary for house owner details.Just for your information, there are two ways to design report,

1. Using Crystal report Editor
2. Using Visual studio Editor

But in this demonstration I using Visual studio editor to create and design report.

Let’s open VS and create new web site called as CrystalReportInASPNET. Then right click the project and select Add New Item from the bar. Select Crystal Report from the templates list window. When you select crystal report, give a meaningful name to the report. Here I have given SampleRpt.rpt.

Image Loading

Now click OK button.desgin wizard is ready, the design wizard look as like below, 

Image Loading

In this window, you have to select which report template format suitable for our report. In this demonstration standard report is 100% suitable. Select and click OK button.Then you need to create the database connection to access tables within the database. Once you click OK button, system gives a window to select the connection type as shown below.

 

Image Loading

This demonstration report is to work with SQL Server 2005 database. So you need to select SQL native client as provider (As above figure shows).


Then click Next button, to continue report creation process. Then you need to assign details of SQL Server and user credential to access the database from the report. Then you need to expand current connection tag, select your database connection then select the relevant table as shown by the following figure.

Image Loading

Then click next select column what columns you are going to display report click finish button, final design will be like followings, 

Image Loading

The report design is finished, now you have to navigate to web page and add CrystalReportViewer on the page to view the page design report.

<%@ Register Assembly="CrystalDecisions.Web, Version=10.5.3700.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"
    Namespace="CrystalDecisions.Web" TagPrefix="CR" %>

<!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">
<body>
    <form id="form1" runat="server" style="background: lightgreen;">
    <table cellpadding="0" cellspacing="0" width="100%">
        <tr>
            <td>
                <CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" AutoDataBind="true" />
            </td>
        </tr>
    </table>
    </form>
</body>
</html>

For add Report Viewer in page, just drag and drop from toolbox and place in page.

The key line of this html code, when you drag and drop the CrystalReportViewer in page, VS automatically registers the ReportViewer. If you delete this line the page will throw error “Tag is missing”.

<%@ Register Assembly="CrystalDecisions.Web, Version=10.5.3700.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"
    Namespace="CrystalDecisions.Web" TagPrefix="CR" %>

Note: Make sure your development pc and server must have same version of Crystal Report assembly in order to work without any issues. Most of the developers made mistake on it.

Then write code set designed report to report object and then set it to CrystalReportViewer.

protected override void OnPreRender(EventArgs e)
{
        // attached our report to viewer and set database login.
         ReportDocument report = new ReportDocument();
         report.Load(Server.MapPath("SampleRpt.rpt"));
         report.SetDatabaseLogon("username", "pwd", @"server", "database");
         rptviewer.ReportSource = report;
 }

The reason for using “PreRender” event is this is the most suitable event for value setting in asp.net, Otherwise we can set in “PageLoad” event too. Here I did not use a button to load report on the viewer, therefore I have used “PreRender” event.
 
On my code just create a report document object and load report from the path. Here we used the Server. MapPath() method to get absolute path from the report file name.

And also you have to clear report object in page unload event to ignore clear momery.

protected override void OnUnload(EventArgs e)
        {
            if (report != null)
            {
                report = null;
            }
            base.OnUnload(e);
        }

Output

Run application and page output will be like below, 

Image Loading

References

http://www.sap.com/solutions/sapbusinessobjects/sme/reporting/crystalreports/index.epx

Conclusion

In this article,you have learned How to Create Report using Crystal Report in ASP.NET.hopes help, thank you for reading.

 
Sign Up to vote for this article
 
About Author
 
youngmurukan
Occupation-Not Provided
Company-Not Provided
Member Type-Senior
Location-Not Provided
Joined date-12 May 2009
Home Page-Not Provided
Blog Page-Not Provided
 
 
Other popularSectionarticles
Comments
By:TaxerDate Of Posted:6/16/2011 10:57:14 AM
Excellent Article
Hi, This is a great article to learn about basic idea crystal report and ASP.NET Combination.Keep it up and publish more that is very help as like these resources.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top