VB.NET: Displaying MySQL Data on Crystal Reports

Crystal Report has been asked several times, especially the question of how to display data from MySQL in Crystal Report. Actually, I'm not really into it. It's more comfortable using rdl or rdlc reporting.  But this is just my personal preference. 

I was using Crystal Report 8.5 for VB6. For VB.NET I used installed CR with its Visual Studio. Like the example in this post, I use Visual Studio 2008 (because it's the last version I get that contains Crystal Report 😁).

I use MySQL Connector, just like this post:

https://rani-irsan.blogspot.com/2019/11/connecting-vbnet-to-mysql-database.html

So I need to add its reference. Right-click on project node -> Add Reference...


Find and choose MySQL.Data. It's on the .NET tab, but if you can't find it choose the "Browse" tab and go to the path that the MySQL connector installed. Then click OK.


Next, add a DataSet to define data structure. Right-click on Project Node --> New Item...


Choose Data --> DataSet, I let it be the default name and then click Add button.


DataSet has already been added. Double click to open its designer. 


Right click on design area of DataSet1 -> Add -> DataTable


In this sample, I use a table named class so I rename it to be class too.


Add 2 columns by right-clicking under its table name --> Add --> Column. 


Name columns as their name on the database table. The class table that I use is below: 



Done with the DataSet, don't forget to Save All. Now we'll add a Crystal Report item. Right-click on node project -> Add --> New Item


ChooseReporting, then Crystal Report. You can rename it if you want, but I just let it be default. Then click Add button.


After CR report added open Field Explorer, Right-Click on Database Fields --> Database Expert...


Under ADO.NET Datasets, Latihan.Dataset1 that we've made before will be displayed. Choose class DataTable, and move it by clicking the arrow button as below image.  


After class DataTable appears under Database Fields, drag its columns into the detail part of the report.  Page Header will be generated automatically. Also add a Text Object to make a report title "Data Kategori".


The next important step is often forgotten. We have to include the report file into the Apps folder and also the installer package. These can be done by setting 2 properties which are:
  • Build Action = Content
  • Copy to Output Directory = Copy Always

Now we need to get a DataTable from the database. I'll use module database module that I've made before for MySQL database, or directly download it from here:

https://drive.google.com/file/d/1Ri7j_hxwj3KOROjcqM4EZUbL9qPCV26T/view?usp=sharing

This is the file that I made for Visual Studio 2008 or earlier. After downloading, place it in the project folder and rename it into  "modDatabase.vb".


Then add this file into the project with Visual Studio by right-clicking on project node --> Add -> Existing Item...


Choose modDatabase.vb and click Add button.


Then it will be on the Solution explorer. Open it and do some changes under GetDatabaseSetting procedure. Change the values of connection variables there. Fit it with your own database connection.


And for the UI design, I only add a button and a CrystalReportViewer into Form1.



Code:

Imports CrystalDecisions.CrystalReports.Engine

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, _
            ByVal As System.EventArgs) Handles Button1.Click
        Dim dt As DataTable = GetDataTable("SELECT classcode, classname FROM class")
        Dim cr As ReportDocument = New ReportDocument()
        cr.Load("CrystalReport1.rpt")

        cr.SetDataSource(dt)
        'cr.VerifyDatabase() 'if needed
        CrystalReportViewer1.ReportSource = cr

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, _
            ByVal As System.EventArgs) Handles MyBase.Load
        GetDatabaseSetting()
    End Sub

End Class

The database things are until you get the DataTable. The highlighted rows are the Crystal Report setting. So you see that whatever the database is and whatever the method to get the database data is, as long as you can get it into the DataTable, you can display it on the report.

So now, let's try to run and click Button 1.



Post a Comment

0 Comments