Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

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

0.00/5 (No votes)
22 Apr 2012 1  
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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here