Click here to Skip to main content
13,139,254 members (83,961 online)
Click here to Skip to main content
Add your own
alternative version


31 bookmarked
Posted 25 Apr 2008

Using Crystal Reports in ASP.Net Making use of strongly typed datasets

, 25 Apr 2008
Rate this:
Please Sign up or sign in to vote.
Using Crystal Reports in ASP.Net Making use of strongly typed datasets


This simple project will demonstrate how to report using crystal reports. In this example strongly typed datasets are used.

To start using the example you must have the Northwinds database. The example uses the customers table from the Northwinds database. Before starting you should start a new web site from visual studio.

Using the Code

Create the Dataset

To create the dataset right click the web site and select “Add New Item” from the menu. From the list select Dataset.(I have named m y dataset “CustomersDS”. Visual studio will automatically put this and all resulting code under the “App_Code” folder.

After the wizard has added the dataset to the App_Code folder, drag the TableAdapter from the toolbox. On the resulting Wizard, create a SQl connection that will point to the Northwind database. (Note: This connection will be saved in your web.config).. Use the wizard to create either sql statements or Stored procedures to select customers..Create the following:

  1. A select * from customers
  2. A select * from customers where Country=@Country

Create another dataset that will select distinct Country from the customers table (this will be used for the drop down for the parameterized report).

Accessing the Data

The next thing is to access the data exposed by the dataset in a typical business logic layer. Right click App_Code and select “Add New item” . select class and add a new class and name it accordingly(in my case I named mine “CustomersBLL”..The next thing will be the logic to access this data, I used properties:

private CustomersTableAdapter _Adapter = null;
public CustomersTableAdapter Adapter //this will expose the customersTableAdapter
                                     // which is in the Customers Dataset
        if (_Adapter == null)
            _Adapter = new CustomersTableAdapter();
        return _Adapter;

The next thing will be to right the different methods to return the data as a datatable and as a dataset:

public CustomersDS.CustomersDataTable GetAllCustomers() //this method will return all
    return Adapter.CustomersGet(); 
public DataSet CustomersDataSetByCountry(string Country)
    DataSet ds = new DataSet();
    DataTable dt = GetCustomersByCountry(Country);
    return ds;

Create the Report

Add the reports folder and right click it and select “Add New Item”.. then select Crystal Report”. On the resulting dialog select “As a blank report”. On the menus select Crystal Report >>Database>>Database Expert..On the resulting dialog select “Create New Connection” then select “ADO.Net”. An Ado.Net Dialog will open, On File Path navigate to where your dataset is located

After clicking Finish, you must be able to see your dataset. Select the Customers dataset. Click the customers dataset and select it so that it’s on the right panel.

On the field Explorer select Database Fields, this will display all the fields from the dataset. You can then drag the necessary fields to the report area. Save the report. Your report now has the correct schema all you need is to supply the data to the report.

Pass data to the Report

On the necessary aspx page, you can then have the code below to get data to the report.

CrystalDecisions.CrystalReports.Engine.ReportDocument rd =
     new CrystalDecisions.CrystalReports.Engine.ReportDocument();
CustomersBLL objBll = new CustomersBLL(); //instantiate the customersBLL
DataSet ds = new DataSet();
ds = objBll.CustomersDataSet(); //get all customer information into a dataset
string path = Server.MapPath(@"Reports\crAllCustomers.rpt"); //path of the crystal report
rd.Load(path); //load the report to the ReportDocument
crAllCustomers.ReportSource = rd;

crAllCustomers being the Crystal Report Viewer control.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior) Ecom Institute
South Africa South Africa
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralCrystal Report Pin
Slick6911-Sep-08 15:12
memberSlick6911-Sep-08 15:12 
GeneralRe: Crystal Report [modified] Pin
Mthabisi Khumalo11-Sep-08 21:59
memberMthabisi Khumalo11-Sep-08 21:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170915.1 | Last Updated 25 Apr 2008
Article Copyright 2008 by Khumza007
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid