Click here to Skip to main content
15,075,633 members
Articles / Database Development / MySQL
Posted 6 May 2006


85 bookmarked

How to Create a Crystal Report using C# and MySQL

Rate me:
Please Sign up or sign in to vote.
3.65/5 (31 votes)
9 Feb 2009CPOL3 min read
This is a sample that guides you to create reports with Crystal Reports using C# and MySQL RDBMS.


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(); 
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 
// 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
Regarding this article you can send any question to or 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

Can you hear the music… 

Says it feels right this time <br />Turned it 'round and found the right line <br />"Good day to be alive, Sir <br />Good day to be alive" he said. <br /><br />"No Leaf Clover" --- Metallica


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


About the Author

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

Comments and Discussions

GeneralMy vote of 5 Pin
Manoj Kumar Choubey5-Apr-12 0:02
professionalManoj Kumar Choubey5-Apr-12 0:02 
GeneralMy vote of 5 Pin
nelson20114-Jul-11 2:42
Membernelson20114-Jul-11 2:42 
GeneralCreate Report without Typed Dataset Pin
Xitij Thool16-Feb-11 5:11
MemberXitij Thool16-Feb-11 5:11 
GeneralMy vote of 4 Pin
Syed Wajahat8-Feb-11 2:42
MemberSyed Wajahat8-Feb-11 2:42 
Generalmuy buen ejemplo Pin
fredyAlarcon14-Dec-09 6:07
MemberfredyAlarcon14-Dec-09 6:07 
QuestionAnd what about using .SetDataSource() & .SetParameterValue() simultaneous Pin
Sepehr Shojaee21-Apr-09 1:16
MemberSepehr Shojaee21-Apr-09 1:16 
AnswerRe: And what about using .SetDataSource() & .SetParameterValue() simultaneous Pin
Dnyaneshwar Vedpathak3-Jan-11 1:27
MemberDnyaneshwar Vedpathak3-Jan-11 1:27 
AnswerRe: And what about using .SetDataSource() & .SetParameterValue() simultaneous Pin
Sepehr Shojaee1-Mar-11 0:28
MemberSepehr Shojaee1-Mar-11 0:28 
GeneralMy vote of 1 Pin
Amir Dabaghian16-Mar-09 23:14
MemberAmir Dabaghian16-Mar-09 23:14 
QuestionMore tables in only one dataset Pin
redcloud11-Dec-08 3:49
Memberredcloud11-Dec-08 3:49 
QuestionDought [modified] Pin
Roopangee Tandon16-Sep-08 2:51
MemberRoopangee Tandon16-Sep-08 2:51 
GeneralGood article. Pin
mlangman10-Jul-07 9:56
Membermlangman10-Jul-07 9:56 
QuestionHow to connect to mysql database using C# Pin
kholiwe3-Apr-07 22:39
Memberkholiwe3-Apr-07 22:39 
GeneralWow :) Pin
DanielBrownAU26-Mar-07 15:34
professionalDanielBrownAU26-Mar-07 15:34 
GeneralMaster-detail reports Pin
WillemM9-Mar-07 5:31
MemberWillemM9-Mar-07 5:31 
GeneralVery good article Pin
nsebastian29-Jan-07 11:29
Membernsebastian29-Jan-07 11:29 
GeneralConnection getting closed Pin
tprashanth8-Nov-06 17:56
Membertprashanth8-Nov-06 17:56 
GeneralVietnamese Pin
xuanhau27-Aug-06 21:15
Memberxuanhau27-Aug-06 21:15 
GeneralRe: Vietnamese Pin
le quang anh - leadershipqa28-Aug-06 3:34
Memberle quang anh - leadershipqa28-Aug-06 3:34 
QuestionWhat are the requirements? Pin
Devil™21-May-06 15:44
MemberDevil™21-May-06 15:44 
Generalhmm Pin
DanielBrownAU7-May-06 20:19
professionalDanielBrownAU7-May-06 20:19 

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.