65.9K
CodeProject is changing. Read more.
Home

How to use Crystal Reports with Access database in C#

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.22/5 (21 votes)

Jun 3, 2005

2 min read

viewsIcon

208738

downloadIcon

5999

An article explaining how to add reports to your home database in your C# application.

Introduction

There are many ways to present data to users. For example, you can write code to loop through records and print them inside your Windows form. However, to do such a code any work beyond basic formatting can be very complicated to program. With Crystal Reports, you can quickly create complex and professional reports. Instead of writing code, you can use Crystal Report Designer interface to create and format reports. Its powerful engine processes the formatting, grouping, and charting criteria that you specify.

Background

I’ve searched well so many sites about code that I can with the help of it, use Crystal Reports to connect to an Access database in a C# application. After searching C# books, I’ve found some nice code that helps me to create this simple application. Hope it can help as a basic architecture.

Using the code

At first, you should simply open VS.NET and then at the File menu, click on New, Project. From the New Project dialog box, choose the Windows Application template project and name it Crystal Report, like shown below:

After creating a window, add to it a Crystal Report Viewer control from the toolbox and dock the control to fit all the windows like shown below:

Select Add New Item from Project menu, click Crystal Report and finally click Open to invoke the Crystal Report Designer as shown below:

Use the Report Expert option and choose a template. For more information about each template, see the MSDN.

Now you should choose the data source as below:

Select Database Files, a dialog box will appear to select the location of the data source. After selecting the data source, insert the table that you want and click Next.

Add the fields you want to report and click Next. On the Chart tab, select the type of chart you want. And click Finish.

Note: You can go to the next steps to specify more options.

Now back to the Windows form. Go to the options of Crystal Report Viewer and specify the ReportSource as the .rpt file that you added in the previous step.

Full code

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
 
namespace Crystal_Report
{
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
        private CrystalDecisions.Windows.Forms.CrystalReportViewer 
                                             crystalReportViewer1;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public Form1()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }

       /// <summary>
       /// Clean up any resources being used.
       /// </summary>
       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.crystalReportViewer1 = new 
                CrystalDecisions.Windows.Forms.CrystalReportViewer();
           this.SuspendLayout();
           // 
           // crystalReportViewer1
           // 
           this.crystalReportViewer1.ActiveViewIndex = -1;
           this.crystalReportViewer1.Dock = System.Windows.Forms.DockStyle.Fill;
           this.crystalReportViewer1.Location = new System.Drawing.Point(0, 0);
           this.crystalReportViewer1.Name = "crystalReportViewer1";
           this.crystalReportViewer1.ReportSource = 
                "C:\\Crystal Report\\CrystalReport2.rpt";
           this.crystalReportViewer1.Size = new System.Drawing.Size(576, 349);
           this.crystalReportViewer1.TabIndex = 0;
           // 
           // Form1
           // 
           this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
           this.ClientSize = new System.Drawing.Size(576, 349);
           this.Controls.Add(this.crystalReportViewer1);
           this.Name = "Form1";
           this.Text = "Form1";
           this.ResumeLayout(false);

       }
       #endregion

       /// <summary>
       /// The main entry point for the application.
       /// </summary>
       [STAThread]
       static void Main() 
       {
           Application.Run(new Form1());
       }
    }
}

Tips

You can modify the report and these sections:

  • Report Header
  • Page Header
  • Details
  • Report Footer
  • Page Footer

Change colors, font, date, style, add more charts…..etc. using the Report tab next to the Windows Form tab.