Click here to Skip to main content
Click here to Skip to main content

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

, 22 Apr 2012
Rate this:
Please Sign up or sign in to vote.
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:

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

Now create a Student class like this:

    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:

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:

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:

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:

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.

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:

        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 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)

About the Author

ashkanpower

United States United States
No Biography provided

Comments and Discussions

 
GeneralMy vote of 5 PinmemberIakovos Karakizas23-Jun-14 2:46 
GeneralMy vote of 4 PinmemberBijay Kant Salotry21-Sep-13 10:51 
QuestionCrystal report on sharepoint list item PinmemberMember 102328201-Sep-13 23:52 
Questionso weird I can't pass the object into crystal report directly PinmemberLam pat23-Jun-12 19:53 
GeneralMy vote of 5 Pinmemberibki16-May-12 16:26 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140709.1 | Last Updated 23 Apr 2012
Article Copyright 2012 by ashkanpower
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid