Click here to Skip to main content
15,891,136 members
Articles / Programming Languages / C#

EasiReports

Rate me:
Please Sign up or sign in to vote.
4.87/5 (64 votes)
13 Feb 2006CPOL6 min read 481.1K   9.7K   219  
A library to add reports to your application.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
using System.Reflection;

using EasiReports;
using EasiReports.Report;
using EasiReports.Metadata;

namespace WindowsApplication1
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		private System.Windows.Forms.Button btnConnection;
		private System.Windows.Forms.Button btnInterrogate;
		private System.Windows.Forms.Button btnNewReport;
		private System.Windows.Forms.Button btnSave;
		private System.Windows.Forms.Button btnLoad;
		private System.Windows.Forms.Button buttonTest;
		private EasiReports.ReportControl _ReportControl;

//		private string m_sConnection = @"Provider=MSDAORA.1;Password=tiger;User ID=scott;Data Source=nickwork";
//		private string m_sConnection = @"Provider=SQLOLEDB;Integrated Security=SSPI;Data Source=DERF;Initial Catalog=Northwind";
		private string m_sConnection = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/Data/Northwind.mdb";
//		private string m_sConnection = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/Data/ETDEMO1.2002.MDB";
//		private string m_sConnection = @"Provider=OraOLEDB.Oracle.1;Data Source=Nickwork;User ID=scott;Password=tiger";

		private CMetadata _Metadata = null;
		private EasiReport _LastSavedReport = null;
		private EasiReport _Report = null;

		public Form1()
		{
			InitializeComponent();

			EasiReports.License.Set( "ApplicationName", "BuiltNicer;4;Evaluation;71633518;4DE53194" );

			_ReportControl.ViewState = ReportControl.EState.Design;
		}

		/// <summary>Clean up any resources being used.</summary>
		/// <param name="disposing"><b>true</b> to release both managed and unmanaged resources; <b>false</b> to release only unmanaged resources.</param>
		/// <remarks>Releases the unmanaged resources and optionally releases the managed resources.</remarks>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.btnInterrogate = new System.Windows.Forms.Button();
			this.btnConnection = new System.Windows.Forms.Button();
			this.btnNewReport = new System.Windows.Forms.Button();
			this.btnSave = new System.Windows.Forms.Button();
			this.btnLoad = new System.Windows.Forms.Button();
			this.buttonTest = new System.Windows.Forms.Button();
			this._ReportControl = new EasiReports.ReportControl();
			this.SuspendLayout();
			// 
			// btnInterrogate
			// 
			this.btnInterrogate.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
			this.btnInterrogate.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnInterrogate.Location = new System.Drawing.Point(24, 344);
			this.btnInterrogate.Name = "btnInterrogate";
			this.btnInterrogate.TabIndex = 1;
			this.btnInterrogate.Text = "Interrogate";
			this.btnInterrogate.Click += new System.EventHandler(this.btnInterrogate_Click);
			// 
			// btnConnection
			// 
			this.btnConnection.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
			this.btnConnection.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnConnection.Location = new System.Drawing.Point(24, 312);
			this.btnConnection.Name = "btnConnection";
			this.btnConnection.TabIndex = 0;
			this.btnConnection.Text = "Connection";
			this.btnConnection.Click += new System.EventHandler(this.btnConnection_Click);
			// 
			// btnNewReport
			// 
			this.btnNewReport.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
			this.btnNewReport.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnNewReport.Location = new System.Drawing.Point(120, 312);
			this.btnNewReport.Name = "btnNewReport";
			this.btnNewReport.TabIndex = 2;
			this.btnNewReport.Text = "New Report";
			this.btnNewReport.Click += new System.EventHandler(this.btnNewReport_Click);
			// 
			// btnSave
			// 
			this.btnSave.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
			this.btnSave.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnSave.Location = new System.Drawing.Point(120, 344);
			this.btnSave.Name = "btnSave";
			this.btnSave.TabIndex = 3;
			this.btnSave.Text = "Save";
			this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
			// 
			// btnLoad
			// 
			this.btnLoad.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
			this.btnLoad.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnLoad.Location = new System.Drawing.Point(120, 376);
			this.btnLoad.Name = "btnLoad";
			this.btnLoad.TabIndex = 4;
			this.btnLoad.Text = "Load";
			this.btnLoad.Click += new System.EventHandler(this.btnLoad_Click);
			// 
			// buttonTest
			// 
			this.buttonTest.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
			this.buttonTest.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.buttonTest.Location = new System.Drawing.Point(216, 344);
			this.buttonTest.Name = "buttonTest";
			this.buttonTest.TabIndex = 5;
			this.buttonTest.Text = "Test";
			this.buttonTest.Click += new System.EventHandler(this.buttonTest_Click);
			// 
			// _ReportControl
			// 
			this._ReportControl.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this._ReportControl.HelpNamespace = "C:\\Program Files\\Microsoft Visual Studio .NET 2003\\Common7\\IDE\\EasiReports.chm";
			this._ReportControl.HelpNavigator = System.Windows.Forms.HelpNavigator.Topic;
			this._ReportControl.Location = new System.Drawing.Point(8, 8);
			this._ReportControl.Name = "_ReportControl";
			this._ReportControl.Size = new System.Drawing.Size(512, 296);
			this._ReportControl.TabIndex = 6;
			this._ReportControl.ViewState = EasiReports.ReportControl.EState.Null;
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(528, 422);
			this.Controls.Add(this._ReportControl);
			this.Controls.Add(this.btnLoad);
			this.Controls.Add(this.btnSave);
			this.Controls.Add(this.btnNewReport);
			this.Controls.Add(this.btnInterrogate);
			this.Controls.Add(this.btnConnection);
			this.Controls.Add(this.buttonTest);
			this.Name = "Form1";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Form1";
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.EnableVisualStyles();
			Application.DoEvents();

			Application.Run(new Form1());
		}

		private void btnConnection_Click(object sender, System.EventArgs e)
		{
			FormEditBox dlg = new FormEditBox( m_sConnection );
			dlg.Text = "Connection";
			if ( dlg.ShowDialog() != DialogResult.OK ) return;
			
			m_sConnection = dlg.TextBoxText;

			if ( _Report != null ) _Report.Connection = m_sConnection;
		}

		private void btnInterrogate_Click(object sender, System.EventArgs e)
		{
			MessageBox.Show( m_sConnection );

			if ( _Metadata == null ) _Metadata = new CMetadata();

			if ( !_Metadata.Interrogate( m_sConnection ) ) 
			{
				MessageBox.Show( "Failed to interrogate database" );
				return;
			}

			if ( _Report != null ) _Report.Metadata = _Metadata;

//			MessageBox.Show( "Interrogation succeeded" );
//			for ( int i = 0 ; i < Metadata.TableCount ; i++ )
//				MessageBox.Show( Metadata.Table( i ).Name );

//			foreach( EasiReports.CMetadata.TableItem table in Metadata.m_htTables.Values )
//				MessageBox.Show( table.Name );

//			EasiReports.CMetadata.TableItem table = Metadata.GetTable( "Employees" );
//			foreach( EasiReports.CMetadata.ColumnInfo column in table.Columns )
//			MessageBox.Show( column.TableName + "." + column.ColumnName );

//			string sPrimaryTableName = "Products";
//			Hashtable htForeignKeys = _Metadata.GetForeignKeys( sPrimaryTableName );
//			foreach( string sForeignTableName in htForeignKeys.Keys )
//			{
//				MessageBox.Show( sForeignTableName );
//				ArrayList alForeignKeys = _Metadata.GetForeignKeys( sPrimaryTableName, sForeignTableName );
//				foreach ( CMetadata.ForeignKeyInfo fki in alForeignKeys )
//					MessageBox.Show( fki.PKTableName + "." + fki.PKColumnName + " = " +
//						fki.FKTableName + "." + fki.FKColumnName );
//			}
		}

		private void btnNewReport_Click(object sender, System.EventArgs e)
		{
			MessageBox.Show( m_sConnection );

			if ( _Metadata == null ) _Metadata = new CMetadata();

			if ( ! _Metadata.Interrogate( m_sConnection ) ) 
			{
				MessageBox.Show( "Failed to interrogate database" );
				return;
			}

			_Report = new EasiReport( m_sConnection, _Metadata );
			ShowReport();
		}

		private void btnSave_Click(object sender, System.EventArgs e)
		{
			SaveFileDialog dlg = new SaveFileDialog();
			dlg.Filter = "Report files (*.EasiReport)|*.EasiReport|All files (*.*)|*.*"  ;

			if ( dlg.ShowDialog() != DialogResult.OK ) return;
				
			_Report.SaveToFile( dlg.FileName );

			_LastSavedReport = new EasiReport( _Report );
		}

		private void btnLoad_Click(object sender, System.EventArgs e)
		{
			OpenFileDialog dlg = new OpenFileDialog();
			dlg.Filter = "Report files (*.EasiReport)|*.EasiReport|All files (*.*)|*.*"  ;

			if ( dlg.ShowDialog() != DialogResult.OK ) return;

			if ( _Report == null ) _Report = new EasiReport();

			_Report.LoadFromFile( dlg.FileName );
			ShowReport();
		}

		protected void ShowReport()
		{
			if ( _Report == null ) { MessageBox.Show( "Null report!" ); return; }

			_ReportControl.SetReport( _Report );

			_LastSavedReport = new EasiReport( _Report );
		}

		private void buttonTest_Click( object sender, System.EventArgs ea )
		{
//			MessageBox.Show( _LastSavedReport.ValueEquals( _Report ) ? "Equal" : "Different" );

//			MessageBox.Show( _Report.metadata.ForeignKeyCount.ToString() );
//			int i = _Report.metadata.GuessRelationships();
//			MessageBox.Show( String.Format( "{0} ForeignKeys added.", i ) );

//			int[] ia = (int[]) Enum.GetValues( typeof( OleDbType ) );
//			string s = String.Empty;
//			foreach ( int i in ia )
//				s += i + " : " + Enum.GetName( typeof( OleDbType ), i ) + "\n";
//			MessageBox.Show( s );

//			_Report = new EasiReport();
//			ShowReport();

//			CMetadata m = new CMetadata();
//			m.Interrogate(
//				"Provider=SQLOLEDB;" +
//				"Integrated Security=SSPI;" +
//	//			"Data Source=DERF;" +
//				"Initial Catalog=Northwind" );
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United Kingdom United Kingdom
I discovered C# and .NET 1.0 Beta 1 in late 2000 and loved them immediately.
I have been writing software professionally in C# ever since

In real life, I have spent 3 years travelling abroad,
I have held a UK Private Pilots Licence for 20 years,
and I am a PADI Divemaster.

I now live near idyllic Bournemouth in England.

I can work 'virtually' anywhere!

Comments and Discussions