Creating your First Crystal Report for Use in a .NET Application
page 4 of 6
by Jeff McWherter
Feedback
Average Rating: 
Views (Total / Last 10 Days): 29091/ 64

Crystal Reports Viewer

Now that the Crystal Reports file has been created, you need to display the report in your project. For our example, we will be using the Crystal Reports ASP.NET controls. The methods outlined here are very similar to the Win Forms controls as well.

To view your newly created Crystal Report file in your custom application, you will need to use the Crystal Report Viewer control as shown in the code below.

Listing 3

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" 
 Inherits="ObjectModel._Default"%>
 
<%@ Register Assembly="CrystalDecisions.Web, Version=12.0.2000.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">
<head runat="server">
    <title>Untitled Page</title>
</head> 
<body>
    <div id="reportMain">
        <asp:Label ID="lblError" CssClass="Error" runat="server" />
        <CR:CrystalReportViewer ID="crvMain" runat="server" />
    </div>
</html>

Now that the control is on the page, we need to “inform” the Crystal Reports Viewer control of the Crystal Reports file. We can do this in the code behind with the code that follows.

Listing 4

ReportDocument employeesReport = new ReportDocument();               
crvMain.ReportSource = employeesReport; 

After this step has been completed, we need set the data for the Crystal Report file that was loaded in the viewer. We simply use the SetDataSource method on the ReportDocument object.

Listing 5

ReportDocument employeesReport = new ReportDocument();
employeesReport.Load(@"\Employees.rpt");
employeesReport.SetDataSource(employees);         
crvMain.ReportSource = employeesReport; 

When all is said and done, you should have some type of method that looks similar to the LoadReport method below.

Listing 6

private void LoadReport()
{
List<Employee> employees = GetEmployeesAsList();
DataSet employeeDataSet = ListToDataset<Employee>(employees);
ReportDocument employeesReport = new ReportDocument();
 
      try
      {
            employeesReport.Load(@"Reports\Employees.rpt");
          employeesReport.SetDataSource(employees);
                
          crvMain.ReportSource = employeesReport; 
      }
      catch (Exception ex)
      {
lblError.Text = string.Format
 ("A system error has occurred while loading the report {0}", ex.Message);
      }
      finally
      {
            employeesReport = null;
      }
}

If you have followed all the steps above, you should now have your Crystal Report rendering in your custom application.


View Entire Article

User Comments

Title: Many thanks   
Name: binam
Date: 2010-10-19 3:29:41 AM
Comment:
Many Many thanks for this article .
Title: A Look at Crystal Reports   
Name: Tahir Yaqoob
Date: 2010-10-11 6:35:28 AM
Comment:
A very nice article for any beginner to crystal reports.... thank's baby!!!!!!
Title: Article   
Name: Shawpnendu
Date: 2010-05-10 8:51:42 AM
Comment:
Very nice for novice developers.
Title: General Help with Crystal   
Name: Elizabeth
Date: 2010-04-27 3:18:06 PM
Comment:
Very helpful. A get started article allowing me to use Crystal without a lot of time spent learning.
Title: Article   
Name: Junaid
Date: 2010-03-22 9:42:00 AM
Comment:
It is such a useful to us. as i am a beginner so i got a very clear idea about Crystal Report.
Title: article   
Name: dave himanshu
Date: 2010-03-22 6:55:26 AM
Comment:
such a good job!!!
Title: deploy crystal report   
Name: john
Date: 2010-03-16 8:06:24 PM
Comment:
Great article. Thanks.

Once I have created a crystal report using visual studio 2008, what do I have to deploy to my server for the report to work on a shared hosting server?

Product Spotlight
Product Spotlight 



Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-24 6:46:38 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search