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;
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";
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:
CrystalReport1 myReport = new CrystalReport1();
myReport.SetDataSource(custDB);
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
|
| | Msgs 1 to 14 of 14 (Total in Forum: 14) (Refresh) | FirstPrevNext |
|
 |
 | And what about using .SetDataSource() & .SetParameterValue() simultaneous |  | Sepehr Shojaee | 2:16 21 Apr '09 |
|
 |
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
|
|
|
|
 |
|
 |
this is not good becuase not work for packages
|
|
|
|
 |
 | More tables in only one dataset |  | redcloud | 4:49 11 Dec '08 |
|
 |
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 Otherwise, putting-in one or more fields from same table (tb0024.COUNT, tb0024.SUM, tb0024.SEL) i get data 
Where is "the bug"?
|
|
|
|
 |
|
 |
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
|
|
|
|
 |
|
 |
Good article.
The only problem you may have with SQL are some of the data types.
Thanks,
|
|
|
|
 |
 | How to connect to mysql database using C# |  | kholiwe | 23:39 3 Apr '07 |
|
 |
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
|
|
|
|
 |
|
 |
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.
Well done indeed!
Daniel Brown Senior Solutiuons Architect
|
|
|
|
 |
 | Master-detail reports |  | WillemM | 6:31 9 Mar '07 |
|
 |
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
WM.
What about weapons of mass-construction? "What? Its an Apple MacBook Pro. They are sexy!" - Paul Watson
|
|
|
|
 |
|
 |
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
|
|
|
|
 |
 | Connection getting closed |  | tprashanth | 18:56 8 Nov '06 |
|
 |
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
|
|
|
|
 |
|
 |
Hi, Your project can show Vienamese? Would you like help me to show Vietnamese Unicode. Thanks
|
|
|
|
 |
|
 |
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.
|
|
|
|
 |
 | What are the requirements? |  | gibsosmat | 16:44 21 May '06 |
|
 |
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
|
|
|
|
 |
|
 |
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
|
|
|
|
 |
|
|