SIGN UP MEMBER LOGIN:    
ARTICLE

Crystal Report Between Two Dates using a Stored Procedure

Posted by Kiran Karale Articles | Crystal Reports C# April 20, 2011
Here you will see how to use Crystal Report between two dates using a Stored Procedure.
Reader Level:
Download Files:
 


Step 1: Open a Windows Forms Application & place 2 DateTimePicker controls and a Button control in the form.

As shown in the form below:

CrystalRepo1.gif

Write this code under a Form.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.ReportSource;
using CrystalDecisions.Shared;
using CrystalDecisions.Windows.Forms;

namespace Student
{
    public partial class DateWise : Form
    {

        public DateWise()
        {
            InitializeComponent();
        }
        private void DateWise_Load(object sender, EventArgs e)
        {
 
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DateReport datereport = new DateReport();
            datereport.toDate = Convert.ToDateTime(cbTodate.Value);
            datereport.FromDate = Convert.ToDateTime(cbFromDate.Value);
            datereport.ShowDialog(); 
        }      
    }
}

Step 2: Creating a report: go to the Solution Explorer and add a new item in LHS Categories choose Reporting 7 in RHS choose Cryatal Rerport Template & give a name for it then click thge Add Button then open a Galary and choose a Blank Report then open a window and go to Field Explorer, choose Database field then right click and select Database Expert.

Wizard choose Create New Connection, explore it choose OLEDB (ADO) for the Provider and select the provider named Microsoft OLEDB Provider For SQL Server / SQL Native Client (only for SQL Server) then click Next and fill in Connection Information & click Finish.

Then the Report Creation Wizard is opened.

Now in the Report Wizard choose Database; explore it 7 choose the Stored Procedure that your want as shown below.

CrystalRepo2.gif

Step 3: Click ok then go to Field Exploorer & choose Stored Procedure; explore it & drag & drop field in Report Sesion (Details) & design it as per your requirements.

Step 4:

If you want to pass a parameter to a report then right-click on Parameters Field in field Explorer. Then click on New.

For e.g.

CrystalRepo3.gif

Now Create a parameter called fromdate as the start date.

CrystalRepo4.gif

Similarly create a ToDate parameter.

CompanyName,Address,Website Follow the above procedure

Step 5: Design the Report as shown in the screen below.

cr1.gif

Step 6:

(launching the Report) Add a new form and place a Crystal Report Viewer Control on it; set the Windows state property to Maximized.

Add a reference for System. Configuration assembly also add "Application Configuration File" i.e.app Config & under it write the following:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <
configSections>
  </
configSections>
  <
connectionStrings>
    <
add name="Student.Properties.Settings.StudentConnectionString"
      connectionString="Data Source=.;Initial Catalog=Student;User ID=sa;Password=p@ssw0rd"
      providerName="System.Data.SqlClient" />
  </connectionStrings>
  <
appSettings >
    <
add key="CName" value="XYZ Software Solution"/>
    <add key="Addr" value=" Address-"/>
    <add key="WebSite" value=" Website:"/>
    <add key="Y5Institute" value="Data Source=C1;Initial Catalog=Student;User ID=sa;Password=p@ssw0rd;"/>
  </appSettings>
</configuration>

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;

namespace Student
{
    public partial class ReportSelection : Form
    {

        internal DateTime ToDate;
        internal DateTime FromDate;
        public ReportSelection()
        {
            InitializeComponent();
        }

        private void ReportSelection_Load(object sender, EventArgs e)
        {
            ConnectionInfo CInfo = new ConnectionInfo();
            CInfo.ServerName = "Connection"// This Should be the same name What we have Configured under Oledb(ADO)
            CInfo.UserID = "sa";
            CInfo.Password = "p@ssw0rd";

            TableLogOnInfo tableInfo = new TableLogOnInfo(); // providing table Details  use the Seperate object of TableLogOnInfo Class for each table we use in the Report
            tableInfo.ConnectionInfo = CInfo;

            TableLogOnInfos tablelog = new TableLogOnInfos();
            tablelog.Add(tableInfo);
            crystalReportViewer1.ReportSource = "F:\\kiran\\Student\\Student\\StudentFees.rpt"; //<"Path of the report">;
            crystalReportViewer1.LogOnInfo = tablelog;

          // Using Above Coding to Skip/Elliminate user Id,passaword i.e.Connection Datails

            string cname = ConfigurationManager.AppSettings.Get("CName");
            string addr = ConfigurationManager.AppSettings.Get("Addr");
            string Website = ConfigurationManager.AppSettings.Get("WebSite");          
            ReportDocument obj = new ReportDocument();
            obj.Load("F:\\kiran\\Student\\Student\\StudentFees.rpt");
            crystalReportViewer1.ReportSource = obj;
            obj.SetParameterValue("CompanyName", cname);
            obj.SetParameterValue("Address", addr);
            obj.SetParameterValue("Company Website", Website);
            obj.SetParameterValue("@StattDate",ToDate);
            obj.SetParameterValue("@EndDate",FromDate); 
            
        }
    }
}

I attached a database (tables & Stored Procedure; execute it).

Note: Please insert data on tables.

Login to add your contents and source code to this article
share this article :
post comment
 

SQL REORTING

Posted by amit singh Dec 08, 2011

Please Send Crystal Report with Asp.net and this artical is asking all the promts from to loginpromt im getting so irritating to seing of those all prompt i want report just on click of button that set but it asking all prompts why it asking is not possible to show report on only click event of button please send me email id is 5qsig1gpg24samaladeepak@gmail.com

Posted by samala deepak Jul 26, 2011

Please Send Crystal Report with Asp.net and this artical is asking all the promts from to loginpromt im getting so irritating to seing of those all prompt i want report just on click of button that set but it asking all prompts why it asking is not possible to show report on only click event of button please send me email id is 5qsig1gpg24samaladeepak@gmail.com

Posted by samala deepak Jul 26, 2011

thanks for comment

Posted by Kiran Karale Apr 21, 2011

Nice article!!!

Posted by Karthikeyan Anbarasan Apr 20, 2011
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Become a Sponsor