Crystal Report in Windows Application


In this article we will discuss about how to create crystal report in windows and what are methods in .net to create Crystal Reports.Creating a Crystal Report allows programmer to create reports from variety of data sources with minimum of written code.Easy to export report in different formats.



Crystal Report in .net support two methods
1) PULL Method:

Using Pull Method ,Crystal Report is directly connected to data source and can retrieve data without providing connection details and SQL commands.This details are configured during designing of Crystal Reports.

2)PUSH Method

Using Push Method,Crystal Report is connected to data-set and data-set to Data-source.To connect to data-source user have to provide connection details and SQL Commands to retrieve data.

Here I am Using PUSH Method to create Crystal reports.
I will show step by step creating Crystal reports.
Step 1:
Install SAP Crystal report and configure with visual studio.

Step 2:
Now go to your project and Right Click on project and select add new Item and Select Crystal report

Add new Crystal report

Step 3:
New Window will open choose Blank Report

create new CR
Step 4:
Now, Add new data set to project & add new table.
Add data set to your project
Step 5:
Go Back to Crystal report and from field explorer tab .right click on 'Database Fields' and database expert following window will open then select data set from list and add to right side

Choose dataset
Step 5:
Design your report as you required and drag and drop required field from data set.
Design CR
Step 6:
Add new form to your project and drag and drop crystal report viewer to your new form
Report Viewer
Step 7:
In code write the following code


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 CrystalDecisions.CrystalReports.Engine;
using System.Data.SqlClient;
using System.Configuration;

namespace CrystalReportSample
{
public partial class Form1 : Form
{
DataTable dt ;
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
//Create new report Document
ReportDocument rptDoc = new ReportDocument();
//Create new instance of Dataset
DataSet1 ds = new DataSet1();
//get Data from db
funGetData();
//merge data in dataset
ds.Tables["TBEMP"].Merge(dt);
//get path of crystal report
string srtPath = Application.StartupPath.ToString() + "\\CrystalReport2.rpt";
rptDoc.Load(srtPath);
rptDoc.SetDataSource(ds);

crystalReportViewer1.ReportSource = rptDoc;
crystalReportViewer1.Show();

}
private DataTable funGetData()
{
dt = new DataTable();
try
{
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationSettings.AppSettings["ConnString"].ToString();
con.Open();
string query = "select * from tbEmp";
SqlCommand cmd = new SqlCommand(query, con);
SqlDataAdapter adp = new SqlDataAdapter(cmd);
adp.Fill(dt);
}
catch (Exception ex)
{
throw ex;
}
return dt;
}
}
}


Step 8:
And don't forget to copy crystal report '.rpt' file in debug and release folder
Step 9:
Run project and check the result here is output
Output

Click Here to download Sample Crystal Report Project


http://www.dotnetspider.com/attachments/Resources/45139-35150-Sample-Project.zip


Attachments

  • Sample Project (45139-35150-Sample-Project.zip)
  • Comments

    No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: