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

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" 
<%@ Register Assembly="CrystalDecisions.Web, Version=12.0.2000.0, Culture=neutral, 
 Namespace="CrystalDecisions.Web" TagPrefix="CR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
<html xmlns="">
<head runat="server">
    <title>Untitled Page</title>
    <div id="reportMain">
        <asp:Label ID="lblError" CssClass="Error" runat="server" />
        <CR:CrystalReportViewer ID="crvMain" runat="server" />

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();
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();
          crvMain.ReportSource = employeesReport; 
      catch (Exception ex)
lblError.Text = string.Format
 ("A system error has occurred while loading the report {0}", ex.Message);
            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
Many Many thanks for this article .
Title: A Look at Crystal Reports   
Name: Tahir Yaqoob
Date: 2010-10-11 6:35:28 AM
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
Very nice for novice developers.
Title: General Help with Crystal   
Name: Elizabeth
Date: 2010-04-27 3:18:06 PM
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
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
such a good job!!!
Title: deploy crystal report   
Name: john
Date: 2010-03-16 8:06:24 PM
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-2021  |  Page Processed at 2021-03-08 8:13:30 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search