Introduction
This is the second tutorial for ReportMax free reporting tool for .NET developers (http://www.cppmax.com). As a pre-requisite, you need to read Tutorial 1. This new tutorial shows the developer how to connect to virtually any data source (in this example Sqlite) by code and without using Ole DB Providers.
Background
If you have your own method of accessing a database, or if you do not have access to an Ole Db provider for that data source, or if you wish to access a custom data source (i.e. XML file), then you need to override SetDataSource
event and write your own code to read from the database and bind it to the report. In this example, I will show how to connect to a Sqlite database using System.Data.Sqlite.dll and fill out the data set for the report to read from.
Using the Code
You will need ReportMax version 2.3. Please make sure this is the version you have even if you have downloaded it yesterday. It is under active development and gets updated frequently.
Report Designer
- Follow the steps to create the report in case you have not done so in Tutorial 1.
- Make sure the ConnectionString and SQL properties are blank.
- Make sure the
ReportFile
property is set to the correct MainReport.rpm path under the sample.
Report Viewer
- Add a reference to System.Data.Sqlite.dll from the sample folder. Since this is a version 2.0 DLL, you need to enable mixed assembly mode. Create or open the SqliteReportMaxSample.exe.config and paste the following code. Place this file where the executable is located:
="1.0" ="utf-8"
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0" />
</startup>
<runtime>
<generatePublisherEvidence enabled="false" />
</runtime>
</configuration>
- Click on the
ReportMaxViewer
control on the Form designer. Click the Events in the Properties Window. Double click Report_SetDataSource
Event. An event handler will be created and the focus will be moved to the code editor.
- Write the following statement at the top of the file:
using System.Data.Sqlite;
- Paste the following code inside
SetDataSource
event handler:
dataset = new DataSet();
string connString = string.Format("Data Source={0};Version=3;", "..\\..\\Cities.db");
SQLiteConnection con = new SQLiteConnection(connString);
con.Open();
string sql = "SELECT Code, CountryName, CityName, City.Population FROM Country,
City WHERE Country.Code = City.Country ORDER BY CountryName;";
SQLiteDataAdapter da = new SQLiteDataAdapter(sql, con);
da.Fill(dataset);
con.Clone();
- Run the application. You should see the report.
Points of Interest
The sample project is provided with this tip.
History