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.
- The code to add current values to the City parameter is used at 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.
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."
Private Const PARAMETER_FIELD_NAME As String = "City"
private const string PARAMETER_FIELD_NAME = "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]
> <P>This declaration is needed to access the ArrayList class.</P>
``` vb
Imports System.Collections
```
``` csharp
using System.Collections;
```
- At the bottom of the class, create a new private method named SetCurrentValuesForParameterField(), with two variables in the method signature: ParameterFields and ArrayList.
``` vb
Private Sub SetCurrentValuesForParameterField(ByVal myParameterFields As ParameterFields, ByVal myArrayList As
ArrayList)
End Sub
```
``` csharp
private void SetCurrentValuesForParameterField(ParameterFields parameterFields, ArrayList arrayList)
{
}
```
- Within this method, declare and instantiate the ParameterValues that are indexed class as the variable currentParameterValues.
> [!NOTE]
> <P>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 <A href="ms227453(v=vs.90).md">Project Setup</A>.)</P>
``` vb
Dim currentParameterValues As ParameterValues = New ParameterValues()
```
``` csharp
ParameterValues currentParameterValues = new ParameterValues();
```
- Create a foreach loop to retrieve all of the submitted values (as type Object) from the ArrayList instance.
> [!NOTE]
> <P>In this method, you retrieve values from the ArrayList. Later you write code that adds values to the ArrayList.</P>
``` vb
For Each submittedValue As Object In myArrayList
Next
```
``` csharp
foreach(object submittedValue in arrayList)
{
}
```
Within the foreach loop, declare and instantiate the ParameterDiscreteValue class.
Dim myParameterDiscreteValue As ParameterDiscreteValue = New ParameterDiscreteValue()
ParameterDiscreteValue parameterDiscreteValue = new ParameterDiscreteValue();
Within the foreach loop, convert the submittedValue to string and pass it to the Value property of the ParameterDiscreteValue instance.
myParameterDiscreteValue.Value = submittedValue.ToString()
parameterDiscreteValue.Value = submittedValue.ToString();
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.
``` vb
currentParameterValues.Add(myParameterDiscreteValue)
```
``` csharp
currentParameterValues.Add(parameterDiscreteValue);
```
Outside the foreach loop, retrieve the ParameterField instance from the ParameterFields indexed class that is based on the index entry of the PARAMETER_FIELD_NAME constant.
Dim myParameterField As ParameterField = myParameterFields(PARAMETER_FIELD_NAME)
ParameterField parameterField = parameterFields[PARAMETER_FIELD_NAME];
Pass the currentParameterValues instance to the CurrentValues property of the ParameterField instance.
myParameterField.CurrentValues = currentParameterValues
parameterField.CurrentValues = currentParameterValues;
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 ParameterField 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, declare and instantiate an ArrayList above the line that binds the report to the CrystalReportViewer control.
Dim myArrayList As ArrayList = New ArrayList()
ArrayList arrayList = new ArrayList();
Add the city names "Paris" and "Tokyo" as strings to the ArrayList instance.
myArrayList.Add("Paris") myArrayList.Add("Tokyo")
arrayList.Add("Paris"); arrayList.Add("Tokyo");
Below the code that binds the CrystalReportViewer control, retrieve the ParameterFields instance from the ParameterFieldInfo property of the CrystalReportViewer control.
Dim myParameterFields As ParameterFields = myCrystalReportViewer.ParameterFieldInfo
ParameterFields parameterFields = crystalReportViewer.ParameterFieldInfo;
Call the SetCurrentValuesForParameterField() method, and pass in the ParameterFields instance, and the ArrayList instance.
``` vb
SetCurrentValuesForParameterField(myParameterFields, myArrayList)
```
``` csharp
SetCurrentValuesForParameterField(parameterFields, arrayList);
```
To test the loading of the CustomersByCity report
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.
- From the Build menu, select Build Solution.
- If you have any build errors, go ahead and fix them now.
- From the Debug menu, click Start.
- Return to Visual Studio and click Stop to exit from debug mode.
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.