Build Reports using Crystal Reports and Stored Procedures



Introduction

Stored Procedures are used to improve the performance of an application when getting data from a database. In this article, a stored procedure is used to get data from the database. The procedure also has two date parameters that are used to specify a date range, from and to.

Crystal Reports in Visual Studio

Crystal Reports is a great business objects tool available in Visual Studio to build reports. Crystal Reports is used for building simple to complex reports including daily reports as sales reports, delivery challans, and stock reports.

Get Started

Let's get started.

Create a new Windows application in Visual Studio and add a new item - Crystal Reports to the Form.

You can right click on the project name in Solution Explorer and select Add New Item and select Crystal Reports from the available items.


CrystalReports1.gif


The complete code is given below.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace SALE_INVENTORY
{
    public partial class frmSALES : Form
    {
        public frmSALES()
        {
            InitializeComponent();
        }

        private void frmSALES_Load(object sender, EventArgs e)
        {
 
        }

        private void fillToolStripButton_Click(object sender, EventArgs e)
        {
            try
            {
                this.spSALESTableAdapter.Fill(this.sP_SALESDATA.spSALES, new System.Nullable<System.DateTime>(((System.DateTime)(System.Convert.ChangeType(startdateToolStripTextBox.Text, typeof(System.DateTime))))), new System.Nullable<System.DateTime>(((System.DateTime)(System.Convert.ChangeType(enddateToolStripTextBox.Text, typeof(System.DateTime))))));
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }
 
        }
     
    }
}

CrystalReports2.gif

Data of Store Procedure has been bind to DataGridView and fill by Query.

CrystalReports3.gif

There is Store Procedure spSALES created in database.

CrystalReports4.gif

Crystal Report format and connection with table.

CrystalReports5.gif

Code on Preview button.

private void btnPrev_Click(object sender, EventArgs e)
      {
            Form frmINVP = new INV_PRINT();
            frmINVP.ShowDialog();
      }

CrystalReports6.gif

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CrystalDecisions.CrystalReports.Engine;
 
namespace SALE_INVENTORY
{
    public partial class INV_PRINT : Form
    {
        public INV_PRINT()
        {
            InitializeComponent();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnPrev_Click(object sender, EventArgs e)
        {
            ReportDocument cryRPT = new ReportDocument();
            cryRPT.Load("C:\\INV.rpt");
            crv1.ReportSource = cryRPT;
            crv1.Refresh();
        }
 
        }
}

CrystalReports7.gif

Now, Crystal Reports works easily within Visual Studio.
Thanks!

Further Readings

Crystal Reports section has many more articles on Crystal Reports development.

Up Next
    Ebook Download
    View all
    Learn
    View all