<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>El Blog de Albert Mata &#187; English</title>
	<atom:link href="http://www.albertmata.net/category/english/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.albertmata.net</link>
	<description>// anotaciones de análisis y programación en mi día a día como consultor de software</description>
	<lastBuildDate>Mon, 03 Aug 2009 19:22:21 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Report in .NET using Crystal Reports and MySQL database.</title>
		<link>http://www.albertmata.net/2008/12/report-in-net-using-crystal-reports-and-mysql-database/#utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=report-in-net-using-crystal-reports-and-mysql-database</link>
		<comments>http://www.albertmata.net/2008/12/report-in-net-using-crystal-reports-and-mysql-database/#comments</comments>
		<pubDate>Tue, 09 Dec 2008 06:00:08 +0000</pubDate>
		<dc:creator>Albert Mata</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Crystal Reports]]></category>
		<category><![CDATA[English]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[bases de datos]]></category>
		<category><![CDATA[connection string]]></category>
		<category><![CDATA[dataset]]></category>
		<category><![CDATA[datatable]]></category>
		<category><![CDATA[informe]]></category>
		<category><![CDATA[odbc]]></category>
		<category><![CDATA[report]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.albertmata.net/?p=148</guid>
		<description><![CDATA[This is just the first of some English posts that I'll publish by translating most popular posts in this blog. Original version (in Spanish) is here.
First of all, I must asume that creating reports is one of that things I like worst in programming. But it's quite obvious that few serious applications don't need them, [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft size-full wp-image-74" title="xmlreport" src="http://www.albertmata.net/public/uploads/2008/07/xmlreport.jpg" alt="" width="230" height="227" />This is just the first of some English posts that I'll publish by translating most popular posts in this blog. Original version (in Spanish) is <a href="http://www.albertmata.net/2008/07/informe-en-net-con-crystal-reports-y-base-de-datos-mysql/" target="_blank">here</a>.</p>
<p>First of all, I must asume that creating reports is one of that things I like worst in programming. But it's quite obvious that few serious applications don't need them, and the one I'm developing now isn't an exception to this rule. So I've been creating some reports recently and I've discovered a new way to do it. And that's what I explain in this post.</p>
<p>As I've said sometimes before in previous posts, I develop with VisualBasic.NET and MySQL database. And I use Crystal Reports to create reports, as this tool is integrated in VisualStudio.NET. Since now, I used to use an ODBC connection configured in each PC to connect to MySQL server. But I didn't like this system much, because actually I'm not working just with one database but with some with different names. They have the same structure, tables and data, but just one is the good one, as the rest are just for developing purposes. It's really easy to use one or other connection string to make the application connect with one or other database, but with reports it isn't so easy as they take data using that ODBC connection (and it just can connect with one database).</p>
<p>But now I've discovered how to create reports with just a DataTable and an XML schema, needing nothing else. Actually it's possible to use a DataSet instead of a DataTable as well. So I'm going to explain it with an easy example and some images.</p>
<p>I'll work with two tables in my MySQL database where I'll keep information about bills. First table is the one with information about headers and has this data:</p>
<div class="code">&#43;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#43;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#43;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#43;<br />
&#124;&#160;blh&#95;num&#160;&#124;&#160;blh&#95;dat&#160;&#160;&#160;&#160;&#124;&#160;blh&#95;cus&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#124;<br />
&#43;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#43;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#43;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#43;<br />
&#124;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1&#160;&#124;&#160;2008&#45;07&#45;30&#160;&#124;&#160;CERAMICAS&#160;PEPE&#44;&#160;S&#46;A&#46;&#160;&#160;&#160;&#160;&#124;<br />
&#124;&#160;&#160;&#160;&#160;&#160;&#160;&#160;2&#160;&#124;&#160;2008&#45;07&#45;30&#160;&#124;&#160;TALLERES&#160;GOMEZ&#44;&#160;S&#46;L&#46;&#160;&#160;&#160;&#160;&#124;<br />
&#124;&#160;&#160;&#160;&#160;&#160;&#160;&#160;3&#160;&#124;&#160;2008&#45;07&#45;31&#160;&#124;&#160;DEPORTES&#160;DAMIAN&#44;&#160;S&#46;L&#46;&#160;&#160;&#160;&#124;<br />
&#124;&#160;&#160;&#160;&#160;&#160;&#160;&#160;4&#160;&#124;&#160;2008&#45;07&#45;31&#160;&#124;&#160;SOFTWARE&#160;ALBERTMATA&#46;NET&#160;&#124;<br />
&#43;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#43;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#43;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#43;</div>
<p>Second table is the one with information about positions and has this rows:</p>
<div class="code">&#43;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#43;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#43;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#43;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#43;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#43;<br />
&#124;&#160;blp&#95;num&#160;&#124;&#160;blp&#95;pos&#160;&#124;&#160;blp&#95;art&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#124;&#160;blp&#95;pri&#160;&#124;&#160;blp&#95;qty&#160;&#124;<br />
&#43;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#43;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#43;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#43;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#43;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#43;<br />
&#124;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1&#160;&#124;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1&#160;&#124;&#160;RATON&#160;LOGITECH&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#124;&#160;&#160;&#160;15&#46;95&#160;&#124;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1&#160;&#124;<br />
&#124;&#160;&#160;&#160;&#160;&#160;&#160;&#160;2&#160;&#124;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1&#160;&#124;&#160;MONITOR&#160;LG&#160;19&#160;PULGADAS&#160;&#124;&#160;&#160;&#160;210&#46;5&#160;&#124;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1&#160;&#124;<br />
&#124;&#160;&#160;&#160;&#160;&#160;&#160;&#160;3&#160;&#124;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1&#160;&#124;&#160;ROUTER&#160;DLINK&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#124;&#160;&#160;&#160;&#160;&#160;&#160;56&#160;&#124;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1&#160;&#124;<br />
&#124;&#160;&#160;&#160;&#160;&#160;&#160;&#160;4&#160;&#124;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1&#160;&#124;&#160;RATON&#160;LOGITECH&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#124;&#160;&#160;&#160;15&#46;95&#160;&#124;&#160;&#160;&#160;&#160;&#160;&#160;&#160;2&#160;&#124;<br />
&#124;&#160;&#160;&#160;&#160;&#160;&#160;&#160;4&#160;&#124;&#160;&#160;&#160;&#160;&#160;&#160;&#160;2&#160;&#124;&#160;TECLADO&#160;LOGITECH&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#124;&#160;&#160;&#160;12&#46;95&#160;&#124;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1&#160;&#124;<br />
&#124;&#160;&#160;&#160;&#160;&#160;&#160;&#160;4&#160;&#124;&#160;&#160;&#160;&#160;&#160;&#160;&#160;3&#160;&#124;&#160;RECEPTOR&#160;GPS&#160;ZAPPA&#160;&#160;&#160;&#160;&#160;&#124;&#160;&#160;&#160;59&#46;95&#160;&#124;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1&#160;&#124;<br />
&#124;&#160;&#160;&#160;&#160;&#160;&#160;&#160;4&#160;&#124;&#160;&#160;&#160;&#160;&#160;&#160;&#160;4&#160;&#124;&#160;PAQUETE&#160;500&#160;FOLIOS&#160;&#160;&#160;&#160;&#160;&#124;&#160;&#160;&#160;&#160;&#160;3&#46;7&#160;&#124;&#160;&#160;&#160;&#160;&#160;&#160;&#160;4&#160;&#124;<br />
&#43;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#43;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#43;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#43;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#43;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#43;</div>
<p>It's something really simple and not <a href="http://en.wikipedia.org/wiki/Database_normalization" target="_blank">normalized</a>, but will be enough for this example, as we're going to create a report that will be the inovice for purchase number 4 (the one with customer SOFTWARE ALBERTMATA.NET). Obviously, we'll need information about both tables but I just want to work with one DataTable, so first of all I'm going to create a MySQL view with this sentence:</p>
<div class="code">CREATE&#160;VIEW&#160;zbl&#95;bill2print&#160;AS&#160;<br />
&#40;<br />
SELECT<br />
&#160;&#160;&#160;&#160;blh&#95;num&#160;AS&#160;BILL&#95;NUMBER&#44;<br />
&#160;&#160;&#160;&#160;blh&#95;dat&#160;AS&#160;BILL&#95;DATE&#44;<br />
&#160;&#160;&#160;&#160;blh&#95;cus&#160;AS&#160;BILL&#95;CUSTOMER&#44;<br />
&#160;&#160;&#160;&#160;blp&#95;pos&#160;AS&#160;LINE&#95;NUMBER&#44;<br />
&#160;&#160;&#160;&#160;blp&#95;art&#160;AS&#160;LINE&#95;ARTICLE&#44;<br />
&#160;&#160;&#160;&#160;blp&#95;pri&#160;AS&#160;LINE&#95;UNITPRICE&#44;<br />
&#160;&#160;&#160;&#160;blp&#95;qty&#160;AS&#160;LINE&#95;UNITS&#44;<br />
&#160;&#160;&#160;&#160;blp&#95;pri&#160;&#42;&#160;blp&#95;qty&#160;AS&#160;LINE&#95;TOTALPRICE<br />
FROM<br />
&#160;&#160;&#160;&#160;blh&#95;billheader&#160;LEFT&#160;JOIN&#160;blp&#95;billposits&#160;ON&#160;blh&#95;num&#160;&#61;&#160;blp&#95;num<br />
WHERE<br />
&#160;&#160;&#160;&#160;blh&#95;num&#160;&#61;&#160;4<br />
&#41;&#59;</div>
<p>So, from now on the report will be created using this zbl_bill2print view. Let's go with the .NET part.</p>
<div class="subtitle">Step 1. Creating XML file containing table/view structure.</div>
<p>Along this post we'll work with these three things:</p>
<p>1) a Windows form (frmMain) where we'll have the report viewer object.<br />
2) a class (clsReportCreator) we're going to create right now.<br />
3) a report (rptBill) that will be the invoice we want to print.</p>
<p>So let's start creating clsReportCreator class. It'll have only one attribute (the name of the table or view), one constructor method, one method to load DataTable object and one last method to generate the XML file. Here is the full code for this class:</p>
<div class="code">&#39;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;<br />
&#39;&#160;Author&#58;&#160;&#160;&#160;&#160;&#160;&#160;Albert&#160;Mata&#160;&#40;www&#46;albertmata&#46;net&#41;<br />
&#39;&#160;Date&#58;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;20080731<br />
&#39;&#160;Needs&#58;&#160;&#160;&#160;&#160;&#160;&#160;&#160;MySQL&#46;Data&#160;reference&#46;<br />
&#39;&#160;Description&#58;&#160;Class&#160;to&#160;create&#160;a&#160;report&#160;using&#160;just&#160;an&#160;XML&#160;file&#46;&#160;<br />
&#39;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;<br />
Imports&#160;MySql&#46;Data&#46;MySqlClient</p>
<p>Public&#160;Class&#160;clsReportCreator</p>
<p>&#160;&#160;&#160;&#160;&#39;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;<br />
&#160;&#160;&#160;&#160;&#39;&#160;Attributes&#46;<br />
&#160;&#160;&#160;&#160;&#39;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;<br />
&#160;&#160;&#160;&#160;Private&#160;TableOrView&#160;As&#160;String</p>
<p>&#160;&#160;&#160;&#160;&#39;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;<br />
&#160;&#160;&#160;&#160;&#39;&#160;Constructor&#160;method&#46;<br />
&#160;&#160;&#160;&#160;&#39;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;<br />
&#160;&#160;&#160;&#160;Public&#160;Sub&#160;New&#40;ByVal&#160;TableOrView&#160;As&#160;String&#41;<br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Me&#46;TableOrView&#160;&#61;&#160;TableOrView<br />
&#160;&#160;&#160;&#160;End&#160;Sub</p>
<p>&#160;&#160;&#160;&#160;&#39;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;<br />
&#160;&#160;&#160;&#160;&#39;&#160;Returns&#160;DataTable&#160;corresponding&#160;to&#160;TableOrView&#46;<br />
&#160;&#160;&#160;&#160;&#39;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;<br />
&#160;&#160;&#160;&#160;Public&#160;Function&#160;GetDataTable&#40;&#41;&#160;As&#160;DataTable<br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Dim&#160;DA&#160;As&#160;MySqlDataAdapter<br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Dim&#160;DS&#160;As&#160;New&#160;DataSet<br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Dim&#160;DT&#160;As&#160;DataTable<br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Dim&#160;ConnectionString&#160;As&#160;String<br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Dim&#160;SQL&#160;As&#160;String</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#39;Setting&#160;connection&#160;string&#160;to&#160;connect&#160;to&#160;MySQL&#160;database&#46;<br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;ConnectionString&#160;&#61;&#160;&#34;Database&#160;&#61;&#160;blog&#59;&#160;&#34;&#160;&#95;<br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#38;&#160;&#34;Data&#160;Source&#160;&#61;&#160;localhost&#59;&#160;&#34;&#160;&#95;<br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#38;&#160;&#34;User&#160;ID&#160;&#61;&#160;root&#59;&#160;&#34;&#160;&#95;<br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#38;&#160;&#34;Password&#160;&#61;&#160;mypassword&#34;</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#39;Setting&#160;SQL&#160;string&#46;<br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;SQL&#160;&#61;&#160;&#34;SELECT&#160;&#42;&#160;FROM&#160;&#34;&#160;&#38;&#160;Me&#46;TableOrView</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#39;Getting&#160;data&#160;and&#160;filling&#160;DataSet&#160;and&#160;DataTable&#46;<br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;DA&#160;&#61;&#160;New&#160;MySqlDataAdapter&#40;SQL&#44;&#160;ConnectionString&#41;<br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;DA&#46;Fill&#40;DS&#44;&#160;Me&#46;TableOrView&#41;<br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;DT&#160;&#61;&#160;DS&#46;Tables&#40;Me&#46;TableOrView&#41;</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#39;Returning&#160;DataTable&#46;<br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Return&#160;DT<br />
&#160;&#160;&#160;&#160;End&#160;Function</p>
<p>&#160;&#160;&#160;&#160;&#39;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;<br />
&#160;&#160;&#160;&#160;&#39;&#160;Creates&#160;XML&#160;file&#160;in&#160;desired&#160;path&#46;<br />
&#160;&#160;&#160;&#160;&#39;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;<br />
&#160;&#160;&#160;&#160;Public&#160;Sub&#160;CreateXMLFile&#40;ByVal&#160;FilePath&#160;As&#160;String&#41;<br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Dim&#160;DT&#160;As&#160;DataTable</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#39;Creating&#160;DataTable&#46;<br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;DT&#160;&#61;&#160;Me&#46;GetDataTable&#40;&#41;</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#39;Writting&#160;XML&#160;file&#160;in&#160;desired&#160;path&#46;<br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;DT&#46;WriteXmlSchema&#40;FilePath&#160;&#38;&#160;Me&#46;TableOrView&#160;&#38;&#160;&#34;&#46;xml&#34;&#41;<br />
&#160;&#160;&#160;&#160;End&#160;Sub</p>
<p>End&#160;Class</p></div>
<p>And we also create frmMain form, which only code by the moment will be this:</p>
<div class="code">&#39;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;<br />
&#39;&#160;Author&#58;&#160;&#160;&#160;&#160;&#160;&#160;Albert&#160;Mata&#160;&#40;www&#46;albertmata&#46;net&#41;<br />
&#39;&#160;Date&#58;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;20080731<br />
&#39;&#160;Description&#58;&#160;Form&#160;to&#160;show&#160;how&#160;to&#160;create&#160;a&#160;report&#160;using&#160;just&#160;an&#160;XML<br />
&#39;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;file&#46;&#160;<br />
&#39;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;<br />
Public&#160;Class&#160;frmMain</p>
<p>&#160;&#160;&#160;&#160;&#39;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;<br />
&#160;&#160;&#160;&#160;&#39;&#160;As&#160;a&#160;first&#160;step&#44;&#160;creates&#160;XML&#160;file&#46;<br />
&#160;&#160;&#160;&#160;&#39;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;<br />
&#160;&#160;&#160;&#160;Private&#160;Sub&#160;frmMain&#95;Load&#40;ByVal&#160;sender&#160;As&#160;System&#46;Object&#44;&#160;&#95;<br />
&#160;&#160;&#160;&#160;ByVal&#160;e&#160;As&#160;System&#46;EventArgs&#41;&#160;Handles&#160;MyBase&#46;Load<br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#39;Creating&#160;XML&#160;file&#46;<br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Dim&#160;RC&#160;As&#160;New&#160;clsReportCreator&#40;&#34;zbl&#95;bill2print&#34;&#41;<br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;RC&#46;CreateXMLFile&#40;&#34;C&#58;&#92;&#34;&#41;<br />
&#160;&#160;&#160;&#160;End&#160;Sub</p>
<p>End&#160;Class</p></div>
<p>Right now we have a first application. If we execute it we'll get C:\zbl_bill2print.xml file with the structure of zbl_bill2print view. So we run it and get <a href="http://www.albertmata.net/files/zbl_bill2print.xml" target="_blank">that file</a>.</p>
<div class="subtitle">Step 2. Creating report and loading data source.</div>
<p>First, we add a report to our project and give it a name like rptBill.rpt. We create it choosing empty report option, so desestimating any templates.</p>
<p>Now we go to Fields explorer menu and right-click the first option (Database fields). In new contextual menu we click on Database assistant option.</p>
<p style="text-align: center;"><img class="alignnone size-full wp-image-75" title="blog_020_1" src="http://www.albertmata.net/public/uploads/2008/07/blog_020_1.jpg" alt="" width="224" height="147" /></p>
<p>After this we get the Available data source menu, where we choose Create new connection and after that ADO.NET option.</p>
<p style="text-align: center;"><img class="alignnone size-full wp-image-76" title="blog_020_2" src="http://www.albertmata.net/public/uploads/2008/07/blog_020_2.jpg" alt="" width="232" height="305" /></p>
<p>Making this, we'll see a new form where we'll be asked about File's path. In this point we have to find XML file we've created before (in my example C:\zbl_bill2print.xml) and then press Finish. We have NewDataSet option including our just added zbl_bill2print in Available data source menu now. </p>
<p style="text-align: center;"><img class="alignnone size-full wp-image-77" title="blog_020_3" src="http://www.albertmata.net/public/uploads/2008/07/blog_020_3.jpg" alt="" width="232" height="305" /></p>
<p>So we select it and press button to move it to Selected tables menu. Done this, it's time to click on Accept.</p>
<p style="text-align: center;"><img class="alignnone size-full wp-image-78" title="blog_020_4" src="http://www.albertmata.net/public/uploads/2008/07/blog_020_4.jpg" alt="" width="231" height="305" /></p>
<p>With all this stuff we've gotten that zbl_bill2print structure available in Fields explorer menu with all its fields, as shown in image below:</p>
<p style="text-align: center;"><img class="alignnone size-full wp-image-79" title="blog_020_5" src="http://www.albertmata.net/public/uploads/2008/07/blog_020_5.jpg" alt="" width="224" height="311" /></p>
<div class="subtitle">Step 3. Designing report.</div>
<p>Nothing special to say here. Just adding fields from Fields explorer menu, inserting text objects where needed, sums, text formats, images and so on...</p>
<p>I've just created a very simple design like this:</p>
<p style="text-align: center;"><img class="alignnone size-full wp-image-80" title="blog_020_6" src="http://www.albertmata.net/public/uploads/2008/07/blog_020_6.jpg" alt="" width="399" height="313" /></p>
<div class="subtitle">Step 4. Last actions to get the invoice.</div>
<p>Finally we're going to create the bill. To do that, we add a CrystalReportViewer object in frmMain form. I call it crvBill. After that it's necessary to modify frmMain source code to make it look like this:</p>
<div class="code">&#39;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;<br />
&#39;&#160;Author&#58;&#160;&#160;&#160;&#160;&#160;&#160;Albert&#160;Mata&#160;&#40;www&#46;albertmata&#46;net&#41;<br />
&#39;&#160;Date&#58;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;20080731<br />
&#39;&#160;Description&#58;&#160;Form&#160;to&#160;show&#160;how&#160;to&#160;create&#160;a&#160;report&#160;using&#160;just&#160;an&#160;XML<br />
&#39;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;file&#46;&#160;<br />
&#39;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;<br />
Imports&#160;CrystalDecisions&#46;CrystalReports&#46;Engine</p>
<p>Public&#160;Class&#160;frmMain</p>
<p>&#160;&#160;&#160;&#160;&#39;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;<br />
&#160;&#160;&#160;&#160;&#39;&#160;Creates&#160;XML&#160;file&#160;&#40;just&#160;once&#41;&#160;and&#160;creates&#160;and&#160;loads&#160;a&#160;report&#46;<br />
&#160;&#160;&#160;&#160;&#39;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;<br />
&#160;&#160;&#160;&#160;Private&#160;Sub&#160;frmMain&#95;Load&#40;ByVal&#160;sender&#160;As&#160;System&#46;Object&#44;&#160;&#95;<br />
&#160;&#160;&#160;&#160;ByVal&#160;e&#160;As&#160;System&#46;EventArgs&#41;&#160;Handles&#160;MyBase&#46;Load<br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#39;Creating&#160;XML&#160;file&#46;<br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Dim&#160;RC&#160;As&#160;New&#160;clsReportCreator&#40;&#34;zbl&#95;bill2print&#34;&#41;<br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#39;RC&#46;CreateXMLFile&#40;&#34;C&#58;&#92;&#34;&#41;</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#39;Creating&#160;report&#46;<br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Dim&#160;RD&#160;As&#160;ReportDocument&#160;&#61;&#160;New&#160;rptBill&#40;&#41;</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#39;Setting&#160;data&#160;source&#160;for&#160;report&#46;<br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Dim&#160;DT&#160;As&#160;DataTable&#160;&#61;&#160;RC&#46;GetDataTable&#40;&#41;<br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;RD&#46;SetDataSource&#40;DT&#41;</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#39;Setting&#160;data&#160;source&#160;for&#160;possible&#160;subreports&#46;<br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;For&#160;Each&#160;SR&#160;As&#160;ReportDocument&#160;In&#160;RD&#46;Subreports<br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;If&#160;SR&#46;Database&#46;Tables&#46;Count&#160;&#62;&#160;0&#160;Then<br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;SR&#46;SetDataSource&#40;DT&#41;<br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;End&#160;If<br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Next</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#39;Setting&#160;recently&#160;created&#160;report&#160;must&#160;be&#160;shown&#160;in&#160;viewer&#46;<br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Me&#46;crvBill&#46;ReportSource&#160;&#61;&#160;RD<br />
&#160;&#160;&#160;&#160;End&#160;Sub</p>
<p>End&#160;Class</p></div>
<p>It's important to note that the line where the XML file is created is commented now, as we just need to create this file once to use it to create the source data, but from now on we don't need to generate it every time.</p>
<p>What we're mainly doing in this code is:</p>
<p>1) creating a report object same kind we've designed in step 3,<br />
2) getting a DataTable with data we want to show (in this example and according to the way we've defined MySQL view, we want to show invoice number 4),<br />
3) setting this DataTable as the report's source data,<br />
4) asking CrystalReportViewer to show this report.</p>
<p>We execute the application again and get desired invoice:</p>
<p style="text-align: center;"><img class="alignnone size-full wp-image-81" title="blog_020_7" src="http://www.albertmata.net/public/uploads/2008/07/blog_020_7.jpg" alt="" width="484" height="416" /></p>
<p>Of course there should be quite more information, images and legal texts in a real invoice, but this is just an easy example of how to do the report itself.</p>
<p>So we've seen how to create a report in VisualBasic.NET just using an XML file. Of course there are plenty of things to improve, as optimizing how database connection is done, or avoiding WHERE condition directly in MySQL view and so on... but what I was looking for with this example was just a very minimum guide to show the process.</p>
<p>PS. Some menu and option names can be different as I develope in VisualStudio Spanish version and I've just translated them as I've thought they could appear in English version. Sorry about that!</p>
<div class="subtitle">Update.</div>
<p> There is a second part for this post <a href="http://www.albertmata.net/2008/10/pasando-parametros-al-informe-en-net-con-crystal-reports/" target="_blank">explaining how to pass parameters from form to report</a>, but it's still only in Spanish.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.albertmata.net/2008/12/report-in-net-using-crystal-reports-and-mysql-database/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
