Setting Parameters Manually in Code
You are now ready to set two values ("Paris" and "Tokyo") into the City parameter field for the CustomersByCity report.
This involves some coding, which you can separate into the following processes:
- You need a PARAMETER_FIELD_NAME constant to hold the "City" parameter field name, which is used several times throughout the code.
- The code to add current values to the parameter is commonly used at two different locations in this tutorial; therefore, you create this code as a separate helper method.
- Within the ConfigureCrystalReports() method, you add the "Paris" and "Tokyo" parameters to an ArrayList instance and pass in both the report and the ArrayList instance to the helper method to be processed.
In the next section, you learn how to retrieve the default values from the parameter field and set those values into a ListBox control. These are used at the end of the tutorial to select new cities dynamically and filter the report based on those newly selected cities.
Continue to Create a ListBox Control that Displays Default Parameters.
To create a PARAMETER_FIELD_NAME constant
- Return to the code-behind class for this Web or Windows Form.
- At the class level, create a new string constant,
PARAMETER_FIELD_NAME, and set its value to "City."
To create a helper method that adds current values to the parameter in the report
You are now ready to create the helper method that adds current values to the parameter in the report.
- Return to the code-behind class for this Web or Windows Form.
-
Above the class signature, add an
"Imports" [Visual Basic] or
"using" [C#] declaration to the top of the
class for the System.Collections namespace (if this namespace has
not already been declared).
Note
This declaration is needed, to access the ArrayList class.
-
At the bottom of the class, create a new private method named
SetCurrentValuesForParameterField(), with
two variables in the method signature: ReportDocument, and
ArrayList.
Note
Later in this tutorial, if you have used an embedded report, you pass your embedded report class into the ReportDocument method parameter. How is this possible? All embedded report classes in Crystal Reports inherit from the ReportDocument base class.
-
Within this method, declare and instantiate the ParameterValues
that are indexed class as the variable currentParameterValues.
Note
For the ParameterValues class to be accessible, you must have included an "Imports" [Visual Basic] or "using" [C#] declaration at the top of the code-behind class for the CrystalDecisions.Shared namespace. (You added this declaration in Project Setup.)
-
Create a foreach loop to retrieve all of the submitted values
(as type Object) from the ArrayList instance.
Note
In this method, you retrieve values from the ArrayList. Later you write code that adds values to the ArrayList.
- Within the foreach loop, declare and instantiate the
ParameterDiscreteValue class.
- Within the foreach loop, convert the submittedValue to string
and pass it to the Value property of the ParameterDiscreteValue
instance.
-
Within the foreach loop, add the ParameterDiscreteValue
instance into the currentParameterValues indexed class.
This completes the code within the foreach loop. You place the remaining code (from the steps that follow) after the foreach loop.
- Outside the foreach loop, retrieve the
ParameterFieldDefinitions indexed class, which comes from the
DataDefinition property of the ReportDocument instance.
- Retrieve the ParameterFieldDefinition instance from the
ParameterFieldDefinitions indexed class that is based on the index
entry of the PARAMETER_FIELD_NAME constant.
- Pass the currentParameterValues instance to the
ApplyCurrentValues method of the ParameterFieldDefinition instance.
To call the SetCurrentValuesForParameterField() method before the report is bound to the CrystalReportViewer control
This step procedure showed you how to create a method that retrieves submitted values from an ArrayList instance and places them as current values into a ParameterFieldDefinition instance. Now, you must call this method before your report is bound to the CrystalReportViewer control, for the report to be aware that it has parameter settings.
-
In the
ConfigureCrystalReports() method, create a
couple of line breaks in the code above the line that binds the
report to the CrystalReportViewer control.
Within these line breaks, you can now enter additional code that modifies the report before it is bound to the viewer.
- Within the line breaks, declare and instantiate an ArrayList.
- Add the city names "Paris" and "Tokyo" as strings to the
ArrayList instance.
-
Call the
SetCurrentValuesForParameterField() method,
and pass in the CustomersByCityReport instance, and the ArrayList
instance.
To test the loading of the CustomersByCity report
The final line of code in the method is code that binds the report to the CrystalReportViewer control.
You are now ready to build and run your project. It is expected that the report displays successfully because there is now code written to set current values into the parameter field.