1. Trang chủ >
  2. Công Nghệ Thông Tin >
  3. Kỹ thuật lập trình >

C# Crystal Reports String parameter

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (1.18 MB, 71 trang )


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

using

using

using

using



System;

System.Windows.Forms;

CrystalDecisions.CrystalReports.Engine;

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(crParameterVal



ues);



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.



C# Crystal Reports Integer parameter

The following section describes the how to pass an Integer Parameter to Crystal

Reports from 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

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#.



In the previous tutorial , we already saw how to pass a String parameter to the

Crystal Reports from C# - C# Crystal Reports String Paramater - and get the

result. This section is almost same as the previous section and the only

difference is that instead of String parameter here we pass an Integer parameter

from C# . So before we start this section you can take look at the previous

section C# Crystal Reports String Paramater , it explain each steps in details

with pictures.

When we pass an Integer parameter , we have to create a new Integer parameter

in the Parameter Fields of Field Explorer. Then we will get the following

screen and fill the fields like in the following picture .



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

the Crystal Reports . For creating selection formula , Right click on Crystal

Reports designer window , select REPORT -> SELECTION FORMULA ->

RECORD .

Then you can see the record Selection Formula Editor. You can make selection

formula from this screen by choosing fields from the lists in the selection

formula editor screen.

Here only the Product table is selected for generating Crystal Reports . We are

making a formula like select the records from Product table whose value is

greater than the input value. For that, first we select the table field that is

Product_price from Product table and then we select the comparison operator

and finally we select our Parameter we created earlier. The following picture

shows how to select fields from lists . Double click each field then it will

automatically selected .



You can close the Selection Formula Editor screen after creating the 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

using

using

using

using



System;

System.Windows.Forms;

CrystalDecisions.CrystalReports.Engine;

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 =

Convert.ToInt32(textBox1.Text);

crParameterFieldDefinitions =

cryRpt.DataDefinition.ParameterFields;

crParameterFieldDefinition =

crParameterFieldDefinitions["Price"];

crParameterValues =

crParameterFieldDefinition.CurrentValues;

crParameterValues.Clear();

crParameterValues.Add(crParameterDiscreteValue);

crParameterFieldDefinition.ApplyCurrentValues(crParameterVal



ues);



crystalReportViewer1.ReportSource = cryRpt;

crystalReportViewer1.Refresh();

}



}



}



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 any price , then you can see the report of

the Product list whose price is greater than or equal to the entered price.



The Crystal Reports showing the result of Product list whose price is greater

than 50.

6. C# Crystal Reports Date parameter

The following section describes how to pass a Date parameter field from C# to

Crystal Reports.

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#.

In the previous sections , we already saw how to pass a string parameter to

Crystal Reports - C# Crystal Reports String Paramater, how to pass an Integer

parameter to Crystal Reports - C# Crystal Reports Integer Paramater and get

the result. This section is very similar to the previous two sections , so before

we begin this section , take a look at the previous two sections.

C# Crystal Reports Integer Paramater

When we pass a Date parameter from C# , we have to create a new date

parameter in the Parameter Fields of Field Explorer. Then we will get the

following screen and fill the fields like in the picture .



After creating the date parameter field , we have to create the selection formula

for the Crystal Reports . For creating selection formula , Right click on Crystal

Reports designer window , select REPORT -> SELECTION FORMULA ->

RECORD .

Then you can see the record Selection Formula Editor. You can make selection

formula from this screen by choosing fields from the lists in the selection

formula editor screen.

Here we have three tables selected for report (ordermaster , orderdetails and

product ) and we are making the formula like , select all records details from

the tables whose order date is greater than the input date parameter. For doing

this you have to select Ordermaster.orderdate , a comparison operator and

parameter date field from selection list of Formula Editor and make the

formula. The following picture shows how to select the fields from formula

editor and make the formula. Double click each field then it will automatically

selected .



You can close Selection Formula Editor screen after creating the 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 .



Xem Thêm
Tải bản đầy đủ (.pdf) (71 trang)

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×