Click here to Skip to main content
Click here to Skip to main content
Go to top

How to Create a Crystal Report using C# and MySQL

, 9 Feb 2009
Rate this:
Please Sign up or sign in to vote.
This is a sample that guides you to create reports with Crystal Reports using C# and MySQL RDBMS.

Introduction

This article is about creating Crystal Reports using Visual C#.NET and MySQL. We all know that MySQL is an open source DBMS and .NET Framework is free. We have used MySQL and Visual Studio .NET 2003 in developing some of our projects with the help of MySQL Connector .NET 1.05. In this article, I don't discuss how to use My SQL Connector .NET 1.05 in details. However, I got some difficulties while creating reports with Crystal Report, since Crystal Report currently doesn't support connecting directly to MySQL. When I was doing this kind of development, I also looked for samples, but I couldn't find one. That's why I wrote this article on The Code Project.

Daniel Brown has to say... "after working with Crystal Reports and MySQL for almost 2 years, I can tell you that it is possible to natively connect to MySQL.

You need to install the MyODBC ODBC adaptor and just use the ODBC connector in Crystal and you can use all of the technology crystal offers".

But I have some problem with Unicode character when using MyODBC ODBC adaptor.

Create a DataSet, the Key of Designing a Report

As stated above, Crystal Report doesn't allow us to make a connection to datasource which is MySQL and we don't have constructing report dynamic on runtime with Crystal Report. So to design a report, the first step is create a DataSet describing the data you want to display in the report.

To create a Dataset, you click the "Add New Item" in the Project Menu. A picture of an added Dataset looks like the following window and can add elements to the Dataset by dragging and dropping elements from the toolbox. You will fill data from the database to this dataset by using a query. (Note: The names of the elements in this Dataset must be equal to the names of the columns specified in query.) The following code uses MySQL Connector .NET 1.0.5 objects to fill the DataSet.

MySqlConnection conn; 
MySqlCommand cmd; 
MySqlDataAdapter adap; 

// Code to get data from database 
conn = new MySqlConnection("Server=localhost; Database=saf; 
	User ID=root; Password=root; charset=utf8;"); conn.Open(); 
cmd = conn.CreateCommand(); 
cmd.CommandText = "SELECT customer_code, 
	customer_name, customer_address FROM Customers"; 
// Create a Dataset and using DataAdapter to fill it 
adap = new MySqlDataAdapter(); 
adap.SelectCommand = cmd; 
CustomerDS custDB = new CustomerDS(); 
custDB.Clear(); 
adap.Fill(custDB, "Customers"); 

Designing a Report with Crystal Report

Now that you have a created Datset, we can use it to fill the report with data, which will be obtained from the DB. As I said before, you can add a Crystal Report to the project by clicking "Add New Item" in the "Project" menu. Then the following window will appear, and you can select your choices and click OK.

In my simple project, I use Report Expert and choose Standard. In the next window, I choose my previous Dataset from ProjectData \ ADO.NET DataSets.

After selecting Table and Fields to display in Report, you will have a Report that looks like the following figure. And at this time, you can design your report interface as you want.

Display Report in the Form

Up to now, you have fully basic components. Then you'll set the report source attribute for the Crystal Report Viewer control to display the report. You can set the report source by using the following code:
// Create a CrystalReport1 object 
CrystalReport1 myReport = new CrystalReport1(); 
// Set the DataSource of the report 
myReport.SetDataSource(custDB); 
// Set the Report Source to ReportView 
crystalReportViewer1.ReportSource = myReport; 

After that, you can start your application and view the report.

Additional Information

To use this example you need to download install MySQL Connector .NET 1.0.5 in http://dev.mysql.com/downloads/connector/net/1.0.html.
Regarding this article you can send any question to lequanganh.aiti@gmail.com or leadershipqa@yahoo.com. I will send you any information that you want for your development.

About Le Quang Anh

Le Quang Anh
AiTi-APTECH Computer Education
Shape Your Future
35/115 Dinh Cong str - Hoang Mai dis - Ha Noi cap
Website:
 www.aiti-aptech.edu.vn
Email: contact@aiti-aptech.edu.vn
Forum: forum.aiti-aptech.edu.vn

Can you hear the music… 

Says it feels right this time 
Turned it 'round and found the right line 
"Good day to be alive, Sir 
Good day to be alive" he said. 

"No Leaf Clover" --- Metallica 

License

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

Share

About the Author

le quang anh - leadershipqa
Web Developer
Vietnam Vietnam
No Biography provided

Comments and Discussions

 
QuestionAnd what about using .SetDataSource() & .SetParameterValue() simultaneous PinmemberSepehr Shojaee21-Apr-09 1:16 
AnswerRe: And what about using .SetDataSource() & .SetParameterValue() simultaneous Pinmemberdnv1987@gmail.com3-Jan-11 1:27 
AnswerRe: And what about using .SetDataSource() & .SetParameterValue() simultaneous PinmemberSepehr Shojaee1-Mar-11 0:28 

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
Web02 | 2.8.140916.1 | Last Updated 10 Feb 2009
Article Copyright 2006 by le quang anh - leadershipqa
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid