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

How to Create a Crystal Report using C# and MySQL

By , 9 Feb 2009
 

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

License

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
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5membermanoj kumar choubey5 Apr '12 - 0:02 
Nice
GeneralMy vote of 5membernelson20114 Jul '11 - 2:42 
Thanks a lot for this piece of code. Appreciate your help.
GeneralCreate Report without Typed DatasetmemberXITIJ_CODE_0416 Feb '11 - 5:11 
Hello,
nice article. I'm using Visual Studio 2008 and Mysql Connector 5.0.9.
Is it possible to create a report Without crating A Typed Dataset since I'm not storing the Data in Database.
For printing purpose I had stored my Billing information in the Generic List ,and customer Name details.
 
Please enlighten me.
Thank you.
GeneralMy vote of 4memberSyed Wajahat8 Feb '11 - 2:42 
Solved my issue
Generalmuy buen ejemplomemberfredyAlarcon14 Dec '09 - 6:07 
felicitaciones me ayudo bastante Thumbs Up | :thumbsup:
QuestionAnd what about using .SetDataSource() & .SetParameterValue() simultaneousmemberSepehr Shojaee21 Apr '09 - 1:16 
Hi,
How can we use .SetDataSource() & .SetParameterValue() at the same time
I have a problem !
When I Use SetDataSource all of parameteres lost
 

rptdoc.SetParameterValue("myPar", "parValye");
rptdoc.SetDataSource(mydataset.Tables[0]);
crystalReportViewer1.ReportSource = rptdoc;
 
After setting data source all of my set parameteres lost !
 
--Peace
--Sepehr Shojaee
AnswerRe: And what about using .SetDataSource() & .SetParameterValue() simultaneousmemberdnv1987@gmail.com3 Jan '11 - 1:27 
hi
have you got sol for this problem
if yes
Plz give me help I am getting the same problem
thank you
AnswerRe: And what about using .SetDataSource() & .SetParameterValue() simultaneousmemberSepehr Shojaee1 Mar '11 - 0:28 
Hey myfriend
I hope it's not too late
 
I found the answer to resort my command lines :
first is set the data source (I)
then i fill the parameters(II)
 
(I)rptdoc.SetDataSource(mydataset.Tables[0]);
(II)rptdoc.SetParameterValue("myPar", "parValye");
crystalReportViewer1.ReportSource = rptdoc;
 

 
Peace,
sepehr Shojaee
GeneralMy vote of 1memberAmir Dabaghian16 Mar '09 - 23:14 
this is not good becuase not work for packages
QuestionMore tables in only one datasetmemberredcloud11 Dec '08 - 3:49 
Hi! I can reproduce this example using only one db table and one dataset.
If i wanna use two or more db tables with the same dataset i get no
data at runtime. This is how i'm trying to fill my dataset
 
string[] tableNames =
{
"tb0024", // contains fields COUNT, SUM, SEL
"tb0036", // contains fields TRIM, DIV, PROD
"tb0056" // contains fields MUL, SUBT
};
 
string[] sqlCommandsStrings =
{
"SELECT * FROM " + tableNames[0],
"SELECT * FROM " + tableNames[1],
"SELECT * FROM " + tableNames[2]
};
 
MySQLConnection conn = _dbc.getNewConnection();
conn.Open();
 
MySQLDataAdapter adap = new MySQLDataAdapter();
 
MyDataSet dataSet = new MyDataSet();
 
MySQLCommand[] sqlCommands = new
MySQLCommand[sqlCommandsStrings.Length];
for (int i = 0; i < sqlCommandsStrings.Length; i++)
{
sqlCommands[i] = (MySQLCommand)conn.CreateCommand();
sqlCommands[i].CommandText = sqlCommandsStrings[i];
 
adap.SelectCommand = sqlCommands[i];
 
adap.Fill(dataSet, tableNames[i]);
}
 
MyReport report = new MyReport();
report.SetDataSource(dataSet);
 
WindowsForm form = new WindowsForm();
form.ReportViewer.ReportSource = report;
form.Show();
 

 
With this code, if i put in MyReport one field for each table
(tb0024.COUNT, tb0036.TRIM. tb0056.MUL), i get no data Frown | :( Otherwise,
putting-in one or more fields from same table (tb0024.COUNT,
tb0024.SUM, tb0024.SEL) i get data Smile | :)
 
Where is "the bug"?
QuestionDought [modified]memberRoopangee Tandon16 Sep '08 - 2:51 
Hello,
 
I have understood all the code but I am facing one difficulty.I am unable to create an instance of the Crystal Report in the .aspx file and am hence not able to enter the dataset value to the Crystal report.I have used the right namespaces.
Thanking you,
Roopangee Tandon
 
Roopangee Tandon
modified on Thursday, November 13, 2008 9:30 AM

GeneralGood article.membermlangman10 Jul '07 - 9:56 
Good article.
 
The only problem you may have with SQL are some of the data types.
 
Thanks,

QuestionHow to connect to mysql database using C#memberkholiwe3 Apr '07 - 22:39 
Hello
 
I am trying to connect to mysql database using c# 2003. My problem is when i click add connection on server explorer it doesn't list mysql which i have i have installed in my computer. am i using the wrong steps for connecting. here are the steps that i follow. 1. On server explorer i right click data connection and choose add connection
2.On the popup Data Link Propeties i go to Provider and i don't know what to choose there, i know for Access database we choose Microsoft Jet 4.0 OLE DB Provider, What do we choose for MySQL.
 
Thanks
Kholiwe
GeneralWow :)memberDaniel@SA26 Mar '07 - 15:34 
Wow, Im famous, someone included me in their artical. I must admit that was a supriuse. Im glad someone reads comments left on thier articals.
 
Smile | :) Well done indeed!
 
Daniel Brown
Senior Solutiuons Architect

GeneralMaster-detail reportsmemberWillemM9 Mar '07 - 5:31 
Although this article is a good start for building crystal reports using MySQL data, I have one comment:
 
There's a lot of information available on crystal reports, but I'm missing information on how to create proper master-detail reports. I see questions on that a lot on the forums here and elswhere on the internet.
 
Maybe it's a good idea to create an article on that too Smile | :)
 
WM.

What about weapons of mass-construction?

"What? Its an Apple MacBook Pro. They are sexy!" - Paul Watson

GeneralVery good articlemembernsebastian29 Jan '07 - 11:29 

As you mention in the beginning of the article, you can find on internet pretty much about this subject. You article help me a lot. You are god damn genius!
 
Best Regards
Nicu
GeneralConnection getting closedmembertprashanth8 Nov '06 - 17:56 
Hi,
I'm using VS 2003 /C#
Nice article. The application is running perfectly.
However when I try to debug the application I get the following error message.
 
'An established connection was aborted by the sotware in your host machine'
 
I got this exception with all applications that use native MySQLClient
(only when I'm debugging through the code)
How to overcome this issue
 
Best Regards,
Prashanth
GeneralVietnamesememberxuanhau27 Aug '06 - 21:15 
Hi,
Your project can show Vienamese? Would you like help me to show Vietnamese Unicode.
Thanks
GeneralRe: Vietnamesememberle quang anh - leadershipqa28 Aug '06 - 3:34 
Yes, no matter in .NET Framework and in my project to display Unicode characters. You can config in mysql and display Vietnam's Unicode-Characters in my project without no problems!!! Best Regard.
QuestionWhat are the requirements?membergibsosmat21 May '06 - 15:44 
Hi,
I am new to this place..
your article is good.. but the application I made having a crystal reports viewer inside it is not working on other computer..
what are the requirements for the other system?
should that other person/client using my application .. install crystal reports?
or is there anyway like windows free support for the crystal report viewer?
what should I do to make the application to run on any computer having Win-XP + .Net installed?
 
thank you
 
Devil™
I am a good boy. --Since 1985
GeneralhmmmemberDaniel@SA7 May '06 - 20:19 
I have 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.
 
Daniel Brown
Enterprise Software Architect

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 10 Feb 2009
Article Copyright 2006 by le quang anh - leadershipqa
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid