How to create a Report with Parameters

To begin, create a report that draws its information from the sample Microsoft
Access database that ships with Crystal Reports that ships with Crystal Reports.



Note Xtreme.mdb is the sample database that is provided with Crystal Reports. To
locate xtreme.mdb on your hard drive for your version of Crystal Reports

1. In Solution Explorer, right-click the project name that is in bold type, point to Add, and then click Add New Item.
2. In the Add New Item dialog box, in the Templates view, select the Crystal Report template.
3. In the Name field, enter the name "CustomersByCity.rpt" and click Add.
4. In the Create New Crystal Report Document panel of the Crystal Reports
Gallery dialog box, select Using a Report Wizard.
5. In the Choose an Expert panel, select Standard, and then click OK.
6. In the Available Data Sources panel of the Standard Report Creation Wizard
window, expand the Create New Connection folder.
7. From the subfolder that opens, expand the ODBC (RDO) folder.
8. In the ODBC (RDO) window, select the correct ODBC DSN entry for your version of
Crystal Reports and then click Finish.
The ODBC (RDO) folder expands and shows the Xtreme Sample Database.
9. Expand the Tables node, double-click the Customer table to move the table to the Selected Tables panel, and then click Next.
10. Expand the Customer table, then CTRL-click Customer Name, Contact Title,
Address1, Contact Last Name and City.
11. Click the > symbol to move these fields into the Fields to Display panel, then click the Next button.
12. In the Available Fields panel, under Report Fields, double-click Customer.City to
move the field into the Group By panel, and then click the Finish button.
The CustomersByCity report is created and loaded into the main window of Visual Studio.
You are now ready to add a parameter named City and populate it with default values.
The Field Explorer must be visible, because it provides access to the various features of
the report, including parameters. To make the Field Explorer visible, from the Crystal
Reports menu, click Field Explorer.

To add a City parameter

1. If the Field Explorer is not visible, on the Crystal Reports toolbar, click Toggle Field View.

Note Another way to display the Field Explorer is to go to the Crystal
Reports menu, and then click Field Explorer.
2. In the Field Explorer, right-click Parameter Fields and select New...
3. In the Create Parameter Field dialog box:
Set the Name to "City."
Set the Prompting Text to "Select one or more cities."
Set Value Type to String
Select Allow multiple values.
Select Discrete value(s).
Click Default Values...
4. In the Set Default Values dialog box:
Set the Browse table to "Customer."
Set the Browse field to "City."
Click >> (the double right arrow) to move the entire cities list into the Default Values list.
5. Click OK to close the Set Default Values dialog box.
6. Click OK to close the Create Parameter Field dialog box.
You have just set the Default Values to contain a large list of cities.

You now use the Select Expert to set a formula that connects the City database column to
your newly created City parameter field.

To connect the City parameter to the City database column

1. On the Crystal Reports toolbar, click Select Expert.
2. In the Choose Field dialog box, under Report Fields, select Customer.City, and
then click OK.
3. In the Select Expert dialog box, within the Customer.City tab, set the dropdown list
to "is equal to."
4. In the new dropdown list that appears to the right, select the first choice in the list,
{?City}, and then click OK.

Note This selection, {?City}, is the City parameter that you created earlier.

5. From the File menu, select Save All.


You are now ready to bind the report to the CrystalReportViewer control and set the city
parameter with two initial values, Paris and Tokyo.

Binding the Report

It is assumed that you have already placed a CrystalReportViewer control on the Web or Windows Form. In the previous steps, you added a CustomersByCity report to the project.
In this section, you bind the file directory path of the CustomersByCity report to the
CrystalReportViewer control. Then you test whether the report displays correctly when
current values have not been set for its parameter field.

To bind the file directory path of the CustomersByCity report to the
CrystalReportViewer control

1. Open the Web or Windows Form.
2. From the View menu, click Code to view the code-behind class for this Web or Windows Form. 3. Locate the Form Load method (Or any other method where you want to place the code).
4. Declare a string variable, name it reportPath, and assign to it a runtime path to the
local report. This path is determined differently for Web Sites and Windows projects:
For a Web Site, pass the name of the local report file as a string parameter into the
Server.MapPath() method. This maps the local report to the hard drive file
directory path at runtime.


[C#]
string reportPath = Server.MapPath("CustomersByCity.rpt");

[Visual Basic]
Dim reportPath As String = Server.MapPath("CustomersByCity.rpt")

For a Windows project, concatenate the Application.StartupPath property with
a backslash and the local report file name. This maps the report to the same
directory as the Windows executable file.

Note At compile time you will copy the report to the directory containing
the executable file.


[C#]
string reportPath = Application.StartupPath + "\\" +
"CustomersByCity.rpt";

[Visual Basic]
Dim reportPath As String = Application.StartupPath & "\" & "CustomersByCity.rpt"

5. Assign the file directory path of the CustomersByCity report to the ReportSource
property of the CrystalReportViewer control.

[C#]
crystalReportViewer.ReportSource = reportPath;

[Visual Basic]
myCrystalReportViewer.ReportSource = reportPath

You are now ready to build and run your project. It is expected that the report loading will fail, because code has not yet been written to set a value for the City parameter field.

To test the loading of the CustomersByCity report

1. From the Build menu, select Build Solution.
2. If you have any build errors, go ahead and fix them now.
3. If you use a non-embedded report in a Windows project, locate the compiled Windows
executable in the \bin\debug\ subdirectory, and then
copy the report to that subdirectory.

Note To have the non-embedded report loaded by the Windows executable
at runtime, the report must be stored in the same directory as the Windows
executable.

4. From the Debug menu, click Start.

Note If you are developing a Web Site in Visual Studio 2005, and this is the
first time you have started debugging, a dialog box appears and states that
the Web.config file must be modified. Click the OK button to enable
debugging.


The CustomersByCity report does not display. It displays after you add a value for the
City parameter field later in this tutorial.

Note Results may vary, depending on the version of Crystal Reports that
you use. In more recent versions, you can see a form requesting that you
provide parameter values for that report. In earlier versions, a "Missing
parameter field current value" exception is thrown. In either case, you must
add further code to create a fully functional application.

5. Return to Visual Studio and click Stop to exit from debug mode.