A step by step tutorial for beginners who is creating their Crystal Reports for the first time in C#.

All C# Crystal Reports Tutorial in this website is based on the following database - crystaldb. So before you begin this section , please take a look at the database structure of crystaldb - Click Here C# crystaldb

Here we are going to create a new Crystal Reports in C# from Product table in the above mentioned database crystalDB. The Product Table has three fields (Product_id,Product_name,Product_price) and we are showing the whole data from Product table to the C# - Crystal Reports project.

Open Visual Studio .NET and select a new C# Windows project.

Now you will get the default Form1.cs.

From the main menu in Visual Studio C# project select PROJECT-->Add New Item . Then Add New Item dialogue will appear and select Crystal Reports from the dialogue box.

Select Report type from Crystal Reports gallery.

Accept the default settings and click OK.

Next step is to select the appropriate connection to your database (here crstaldb). Here we are going to select OLEDB Connection for SQL Server to connect Crystal Reports in C#.

Select OLE DB (ADO) from Create New Connection .

Select Microsoft OLE DB Provider for SQL Server .

The next screen is the SQL Server authentication screen for connecting to the database - crystalDB. Select your Sql Server name , enter userid , password and select your Database Name .

Click next , Then the screen shows OLE DB Property values , leave it as it is , and then click finish button.

After you click the finish button , the next window you will get your Server name under OLEDB Connection, from there selected database name (Crystaldb) and click the tables , then you can see all your tables from your database.

From the tables list double click the Product table then you can see the Product table will come in the right side list.

Click Next Button

Select all fields from Product table to the right side list .

Click Finish Button. Then you can see the Crystal Reports designer window in your C# project. In the Crystal Reports designer window you can see the selected fields from Product table. You can arrange the field Objects and design of the screen according your requirements. After that your screen is look like the following picture.

Now the designing part is over and the next step is to call the Crystal Reports in your C# application and view it through Crystal Reports Viewer control in C#.

Select the default form (Form1.cs) you created in C# and drag a button and a CrystalReportViewer control to your form .

After you drag the CrystalReportViewer to your form , it will look like the following picture.

You have to include CrystalDecisions.CrystalReports.Engine in your C# Source Code.

using CrystalDecisions.CrystalReports.Engine;

Copy and paste the following source code and run your C# project

Next:C# Crystal Reports from multiple tables

Download Source Code

Print Source Code

using System;

using System.Windows.Forms;

using CrystalDecisions.CrystalReports.Engine;

namespace WindowsApplication1

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)

{

ReportDocument cryRpt = new ReportDocument();

cryRpt.Load(PUT CRYSTAL REPORT PATH HERE\\CrystalReport1.rpt");

crystalReportViewer1.ReportSource = cryRpt;

crystalReportViewer1.Refresh();

}

}

}

NOTES:
cryRpt.Load(PUT CRYSTAL REPORT PATH HERE\\CrystalReport1.rpt");
The Crystal Reports file path in your C# project file location, there you can see CrystalReport1.rpt . So give the full path name of Crystal Reports file like c:\projects\crystalreports\CrystalReport1.rpt
When you run the source code you will get the report like the following picture.

When you click the button, the application will ask the username and password. Later in this tutorial you can find how to avoid asking username and password - C# Dynamic logon parameters in Crystal Reports.
Hope this tutorial help you to create your first Crystal Reports in C#.

Crystal Report From Multiple Table Data

The following section describes how to create Crystal Report from multiple tables in C#.

All C# Crystal Reports Tutorial in this website is based on the following database - crystaldb. So before you begin this section , please take a look at the database structure of crystaldb - Click Here C# crystaldb

Here we are going to generate Crystal Reports from multiple tables in C#. Here we have three tables (ordermaster , orderdetails and product ) and we are generating a Crystal Report from these three tables by connecting each table with their related fields.

If you are new to Crystal Reports and do not know how to create Crystal Reports from C# , please take a look at the section step by step tutorial for creating a Crystal Reports from C#.

Hope you understand the basics of generating a Crystal Reports in C# , this section is the continuation of the first part, so here we avoid some basic steps and start from the table selection of Crystal Reports.

Select all table from the table list to right side list box, because we are creating report from three tables ( OrderMaster, OrderDetails, Product) . If you don't know up to this part of the tutorial , refer previous tutorial for up to selecting databese for Crystal reports.

The next step is to make relations between these selected tables. Here we are connecting the related fields from each table. For that we arrange the tables in visible area in the list (this is not necessary ) and select the fields that we want to make relation and drag to the related field of the other selected tables. After made the relations with tables the screen is look like the following picture .

Next step is to select the fields from the selected tables ( OrderMaster, OrderDetails, Product) . Here we are selecting the fields Customername , orderdate from ordermastertable , Productname from product table and quantity from order details table. The field selection screen is look like the following picture .

After select the fields from tables, click the Finish button because now we are not using any other functionalities of the Crystal Reports wizard. After that you will get the Crystal Reports designer window . You can arrange the fields in the designer window according to your requirement to view the report .

For re-arranging fields in the designer window , you can drag the field object on the screen . For editing right click the field object and select Edit Text Object. The following picture shows the sample of designer window after rearrange the field.

Now the designing part is over and the next step is to call the Crystal Reports in C# and view it in Crystal Reports Viewer control .

Select the default form (Form1.cs) you created in C# and drag a button and a CrystalReportViewer control to your form .

After you drag the CrystalReportViewer to your form , it will look like the following picture.

You have to include CrystalDecisions.CrystalReports.Engine in your C# Source Code.

using CrystalDecisions.CrystalReports.Engine;

Copy and paste the following source code and run your C# project

Next:C# Crystal Reports String parameter

Download Source Code

Print Source Code

using System;

using System.Windows.Forms;

using CrystalDecisions.CrystalReports.Engine;

namespace WindowsApplication1

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)

{

ReportDocument cryRpt = new ReportDocument();

cryRpt.Load(PUT CRYSTAL REPORT PATH HERE\\CrystalReport1.rpt");

crystalReportViewer1.ReportSource = cryRpt;

crystalReportViewer1.Refresh();

}

}

}

NOTES:
cryRpt.Load(PUT CRYSTAL REPORT PATH HERE\\CrystalReport1.rpt");
The Crystal Reports file path in your C# project files location, there you can see CrystalReport1.rpt . So give the full path name of Crystal Reports file like c:\projects\crystalreports\CrystalReport1.rpt
After you run the source code you will get the report like this.

When you click the button, the application will ask the username and password. Later in this tutorial you can find how to avoid asking username and password - C# Dynamic logon parameters in Crystal Reports.

The following section describes how to pass a String parameter to Crystal Reports from C# application.

All C# Crystal Reports Tutorial in this website is based on the following database - crystaldb. So before you begin this section , please take a look at the database structure of crystaldb - Click Here C# crystaldb

If you are new to Crystal Reports and do not know how to create Crystal Reports from C# , please take a look at the section step by step tutorial for creating a Crystal Reports from C#.

Here we pass a String parameter from C# to Crystal Reports . For example , from C# program we pass a customer name as a parameter and show all the orders of that customer to the Crystal Reports.

In the previous tutorial we saw how to generate a C# - Crystal Reports from multiple tables. This program is the continuation of previous tutorial , the only difference is that we pass a Customer Name as String parameter and get the report of that particular Customer only . Before starting to this section just take a look at the previous section C# Crystal Reports from multiple tables.

In the previous section we are getting the report of all orders from all customers , that is , all orders placed by all customers . In this section we pull out the selected customer report only by passing Customer name as argument.

Hope you understand the previous section well, if not, please click here C# Crystal Reports from multiple tables.

Next step is to create a String parameter in Crystal report designer window .

Select the Field Explorer from CrystalReport Menu. (Up to here explained in detail in the previous section C# Crystal Reports from multiple tables)

Then you can see Field Explorer in the Left hand side.

Select Parameter Field from Field Explorer and right Click on Parameter Field.

Fill the appropriate name for Name and Prompting text fields like the following picture.

After creating the parameter field , we have to create the selection formula for the Crystal Reports parameter.

For creating selection formula , Right click on Crystal Reports designer window , select REPORT -> SELECTION FORMULA -> RECORD .

Now you can see the record Selection Formula Editor Screen. For creating selection formula , you have to select the fields from above field list and make the formula .

First you have to select OrderMaster.OrderMaster_customername from Report Field and select the comparison operator and select the parameter field. Double click each field then it will be selected.

Form the following picture you can understand how to select fields.

You can close Selection Formula Editor screen after creating selection formula.

Now the designing part is over and the next step is to call the Crystal Reports in C# and view it in Crystal Reports Viewer control .

Select the default form (Form1.cs) you created in C# and drag a button and a CrystalReportViewer control to your form .

You have to include CrystalDecisions.CrystalReports.Engine in your C# Source Code.

using CrystalDecisions.CrystalReports.Engine;

using CrystalDecisions.Shared;

Copy and paste the following source code and run your C# project

Next:C# Crystal Reports Integer parameter

Download Source Code

Print Source Code

using System;

using System.Windows.Forms;

using CrystalDecisions.CrystalReports.Engine;

using CrystalDecisions.Shared;

namespace WindowsApplication1

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)

{

ReportDocument cryRpt = new ReportDocument();

cryRpt.Load("PUT CRYSTAL REPORT PATH HERE\CrystalReport1.rpt");

ParameterFieldDefinitions crParameterFieldDefinitions ;

ParameterFieldDefinition crParameterFieldDefinition ;

ParameterValues crParameterValues = new ParameterValues();

ParameterDiscreteValue crParameterDiscreteValue = new ParameterDiscreteValue();

crParameterDiscreteValue.Value = textBox1.Text;

crParameterFieldDefinitions = cryRpt.DataDefinition.ParameterFields;

crParameterFieldDefinition = crParameterFieldDefinitions["Customername"];

crParameterValues = crParameterFieldDefinition.CurrentValues;

crParameterValues.Clear();

crParameterValues.Add(crParameterDiscreteValue);

crParameterFieldDefinition.ApplyCurrentValues(crParameterValues);

crystalReportViewer1.ReportSource = cryRpt;

crystalReportViewer1.Refresh();

}

}

}

NOTE :
cryRpt.Load(PUT CRYSTAL REPORT PATH HERE\\CrystalReport1.rpt");
The Crystal Reports file path in your C# project files location, there you can see CrystalReport1.rpt . So give the full path name of Crystal Reports file like c:\projects\crystalreports\CrystalReport1.rpt
Now you can run the program . Enter a Customer Name(enter any existing customer from Ordermaster table) and click the button , then your report is look like the following picture.

Here we get the result of the Customer4's order details.

C# CRYSTAL REPORT STRING PARAMETER

The following section describes how to pass a String parameter to Crystal Reports from C# application.

All C# Crystal Reports Tutorial in this website is based on the following database - crystaldb. So before you begin this section , please take a look at the database structure of crystaldb - Click Here C# crystaldb

If you are new to Crystal Reports and do not know how to create Crystal Reports from C# , please take a look at the section step by step tutorial for creating a Crystal Reports from C#.

Here we pass a String parameter from C# to Crystal Reports . For example , from C# program we pass a customer name as a parameter and show all the orders of that customer to the Crystal Reports.

In the previous tutorial we saw how to generate a C# - Crystal Reports from multiple tables. This program is the continuation of previous tutorial , the only difference is that we pass a Customer Name as String parameter and get the report of that particular Customer only . Before starting to this section just take a look at the previous section C# Crystal Reports from multiple tables.

In the previous section we are getting the report of all orders from all customers , that is , all orders placed by all customers . In this section we pull out the selected customer report only by passing Customer name as argument.

Hope you understand the previous section well, if not, please click here C# Crystal Reports from multiple tables.

Next step is to create a String parameter in Crystal report designer window .

Select the Field Explorer from CrystalReport Menu. (Up to here explained in detail in the previous section C# Crystal Reports from multiple tables)

Then you can see Field Explorer in the Left hand side.

Select Parameter Field from Field Explorer and right Click on Parameter Field.

Fill the appropriate name for Name and Prompting text fields like the following picture.

After creating the parameter field , we have to create the selection formula for the Crystal Reports parameter.

For creating selection formula , Right click on Crystal Reports designer window , select REPORT -> SELECTION FORMULA -> RECORD .

Now you can see the record Selection Formula Editor Screen. For creating selection formula , you have to select the fields from above field list and make the formula .

First you have to select OrderMaster.OrderMaster_customername from Report Field and select the comparison operator and select the parameter field. Double click each field then it will be selected.

Form the following picture you can understand how to select fields.

You can close Selection Formula Editor screen after creating selection formula.

Now the designing part is over and the next step is to call the Crystal Reports in C# and view it in Crystal Reports Viewer control .

Select the default form (Form1.cs) you created in C# and drag a button and a CrystalReportViewer control to your form .

You have to include CrystalDecisions.CrystalReports.Engine in your C# Source Code.

using CrystalDecisions.CrystalReports.Engine;

using CrystalDecisions.Shared;

Copy and paste the following source code and run your C# project

Next:C# Crystal Reports Integer parameter

Download Source Code

Print Source Code

using System;

using System.Windows.Forms;

using CrystalDecisions.CrystalReports.Engine;

using CrystalDecisions.Shared;

namespace WindowsApplication1

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)

{

ReportDocument cryRpt = new ReportDocument();

cryRpt.Load("PUT CRYSTAL REPORT PATH HERE\CrystalReport1.rpt");

ParameterFieldDefinitions crParameterFieldDefinitions ;

ParameterFieldDefinition crParameterFieldDefinition ;

ParameterValues crParameterValues = new ParameterValues();

ParameterDiscreteValue crParameterDiscreteValue = new ParameterDiscreteValue();

crParameterDiscreteValue.Value = textBox1.Text;

crParameterFieldDefinitions = cryRpt.DataDefinition.ParameterFields;

crParameterFieldDefinition = crParameterFieldDefinitions["Customername"];

crParameterValues = crParameterFieldDefinition.CurrentValues;

crParameterValues.Clear();

crParameterValues.Add(crParameterDiscreteValue);

crParameterFieldDefinition.ApplyCurrentValues(crParameterValues);

crystalReportViewer1.ReportSource = cryRpt;

crystalReportViewer1.Refresh();

}

}

}

NOTE :
cryRpt.Load(PUT CRYSTAL REPORT PATH HERE\\CrystalReport1.rpt");
The Crystal Reports file path in your C# project files location, there you can see CrystalReport1.rpt . So give the full path name of Crystal Reports file like c:\projects\crystalreports\CrystalReport1.rpt
Now you can run the program . Enter a Customer Name(enter any existing customer from Ordermaster table) and click the button , then your report is look like the following picture.

Here we get the result of the Customer4's order details.

CRYSTAL REPORT WITH INTEGER PARAMETER