Click here to Skip to main content
15,881,248 members
Articles / Programming Languages / C#
Tip/Trick

Create Crystal Report Document Dynamically from List of Objects in Applications without Database

Rate me:
Please Sign up or sign in to vote.
4.73/5 (10 votes)
22 Apr 2012CPOL2 min read 141.2K   14   6
How to create Crystal report document dynamically from a list of objects in applications without database

Introduction

This tip discusses how to create a report with Crystal Reports without the use of database, and directly from a list of objects in our application.

Using Crystal report with database is easy, but what if somebody wants to gather information from forms and make a report. In this tip, I show how to make classes and make a report out of them.

Getting Ready

This tip is based on Visual Studio 2010 environment and Crystal report for VS 2010, so make sure you have them ready.

What are we going to do?

We are going to create an application, create some student objects and make a report with the entered data.

Let's Get Started

First, create a Crystal report project:

Image 1

In the next window, select "As a blank report".

Now create a Student class like this:

C#
class Student
{
    public Student(string Code, string Name, DateTime BirthDate)
    {
        this.Code = Code;
        this.Name = Name;
        this.BirthDate = BirthDate;
    }

    public string Code { set; get; }
    public string Name { set; get; }
    public DateTime BirthDate { set; get; }
    public int Age
    {
        get
        {
            int now = DateTime.Now.Year;
            int birth = BirthDate.Year;

            return now - birth;
        }
    }
}

We want to make a report for list of students for their Code, Name and Age.

In Form1, we create a list of students manually:

C#
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    
    List<Student> GetStudentList()
    {
        Student s1 = new Student("1000001", "Ashkan Power", DateTime.Parse("17/7/1989"));
        Student s2 = new Student("1000002", "Peter Griffin", DateTime.Parse("10/2/1960"));
        Student s3 = new Student("1000003", "Super Man", DateTime.Parse("1/1/1980"));
        Student s4 = new Student("1000004", "Jim Carry", DateTime.Parse("14/5/1985"));
        Student s5 = new Student("1000005", "Bill Carter", DateTime.Parse("25/8/1940"));
        
        return new List<Student>() { s1, s2, s3, s4, s5 };
    }
}

Now let's make the report:

Open "CrystalReport1.rpt", it's already created in your project.

Customize your report as you like, mine looks like this:

Image 2

It's time to make a Dataset:

  • Right click on your project / Add / New Item
  • Choose data from left / select Dataset and insert your Name

Now in your DataSet, add a new DataTable named "Students".

Add 3 columns "Code", "Name" and "Age". It should look like this:

Image 3

In your report in field explorer, right click on Database fields / database expert, then in opened window, select project data / ADO.NET datasets / [your dataset name] and add it to the right list:

Image 4

Now open database fields and you'll see Students table and fields,

Drag and drop each field in its own place in the Detailed section.

Insert a Record Number from special fields in the number column, and delete title created by columns if you like.

Image 5

Ok, now we just need to show the report on the form, and give the report the students' list.

In Form1, in the constructor or loading event listener, you can pass an instance of the report to your reportviewer, and the list of students as a datasource:

C#
public Form1()
{
    InitializeComponent();

    CrystalReport1 cr = new CrystalReport1();
    crystalReportViewer1.ReportSource = cr;

    cr.SetDataSource(GetStudentList());
}

Everything looks fine. You can run the application and see the result.

But you may get a FileNotFoundException with error string "Could not load file or assembly 'file:///C:\Program Files\SAP BusinessObjects\Crystal Reports for .NET Framework 4.0\Common\SAP BusinessObjects Enterprise XI 4.0\win32_x86\dotnet1\crdb_adoplus.dll' or one of its dependencies. The system cannot find the file specified."

You can fix this by changing the app.config file of the project like this:

(If you have no app.config, add it from addItem / General / application configuration file to your project.)

XML
<?xml version="1.0"?>
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true"><supportedRuntime version="v4.0" 
sku=".NETFramework,Version=v4.0"/></startup>
</configuration> 

That will do it.

License

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


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionAbout the data from different tables Pin
Srikanth54554515-Jun-15 16:12
Srikanth54554515-Jun-15 16:12 
GeneralMy vote of 5 Pin
Iakovos Karakizas23-Jun-14 2:46
professionalIakovos Karakizas23-Jun-14 2:46 
GeneralMy vote of 4 Pin
Bijay Kant Salotry21-Sep-13 10:51
Bijay Kant Salotry21-Sep-13 10:51 
QuestionCrystal report on sharepoint list item Pin
Member 102328201-Sep-13 23:52
Member 102328201-Sep-13 23:52 
Questionso weird I can't pass the object into crystal report directly Pin
Lam pat23-Jun-12 19:53
Lam pat23-Jun-12 19:53 
GeneralMy vote of 5 Pin
ibki16-May-12 16:26
ibki16-May-12 16:26 

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.