IntroductionIn 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 DatabaseLet’s create simple database with one table for this demonstration. The database structure will be like followings, The database name is called HouseRental with HouseOwnerInfo table. There are eight columns inside the HouseOwnerInfo. ImplementationNow 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. Now click OK button.desgin wizard is ready, the design wizard look as like below, 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. 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. Then click next select column what columns you are going to display report click finish button, final design will be like followings, 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, Referenceshttp://www.sap.com/solutions/sapbusinessobjects/sme/reporting/crystalreports/index.epx ConclusionIn this article,you have learned How to Create Report using Crystal Report in ASP.NET.hopes help, thank you for reading. |