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

  1. Return to the code-behind class for this Web or Windows Form.

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

  1. Return to the code-behind class for this Web or Windows Form.

  2. 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).

``` vb
Imports System.Collections
```

``` csharp
using System.Collections;
```


> [!NOTE]
> <P>This declaration is needed, to access the ArrayList class.</P>
  1. At the bottom of the class, create a new private method named SetCurrentValuesForParameterField(), with two variables in the method signature: ReportDocument, and ArrayList.
> [!NOTE]
> <P>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.</P>


``` vb
Private Sub SetCurrentValuesForParameterField(ByVal myReportDocument As ReportDocument, ByVal myArrayList As ArrayList)

End Sub
```

``` csharp
private void SetCurrentValuesForParameterField(ReportDocument reportDocument, ArrayList arrayList)
{
}
```
  1. 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();
```
  1. 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)
{
}
```
  1. Within the foreach loop, declare and instantiate the ParameterDiscreteValue class.

    Dim myParameterDiscreteValue As ParameterDiscreteValue = New ParameterDiscreteValue()
    
    ParameterDiscreteValue parameterDiscreteValue = new ParameterDiscreteValue();
    
  2. 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();
    
  3. Within the foreach loop, add the ParameterDiscreteValue instance into the currentParameterValues indexed class.

``` vb
currentParameterValues.Add(myParameterDiscreteValue)
```

``` csharp
currentParameterValues.Add(parameterDiscreteValue);
```

This completes the code within the foreach loop. You place the remaining code (from the steps that follow) after the foreach loop.
  1. Outside the foreach loop, retrieve the ParameterFieldDefinitions indexed class, which comes from the DataDefinition property of the ReportDocument instance.

    Dim myParameterFieldDefinitions As ParameterFieldDefinitions = myReportDocument.DataDefinition.ParameterFields
    
    ParameterFieldDefinitions parameterFieldDefinitions = reportDocument.DataDefinition.ParameterFields;
    
  2. Retrieve the ParameterFieldDefinition instance from the ParameterFieldDefinitions indexed class that is based on the index entry of the PARAMETER_FIELD_NAME constant.

    Dim myParameterFieldDefinition As ParameterFieldDefinition = myParameterFieldDefinitions(PARAMETER_FIELD_NAME)
    
    ParameterFieldDefinition parameterFieldDefinition = parameterFieldDefinitions[PARAMETER_FIELD_NAME];
    
  3. Pass the currentParameterValues instance to the ApplyCurrentValues method of the ParameterFieldDefinition instance.

    myParameterFieldDefinition.ApplyCurrentValues(currentParameterValues)
    
    parameterFieldDefinition.ApplyCurrentValues(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 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.

  1. 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.
  1. Within the line breaks, declare and instantiate an ArrayList.

    Dim myArrayList As ArrayList = New ArrayList()
    
    ArrayList arrayList = new ArrayList();
    
  2. 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");
    
  3. Call the SetCurrentValuesForParameterField() method, and pass in the CustomersByCityReport instance, and the ArrayList instance.

``` vb
SetCurrentValuesForParameterField(customersByCityReport,
myArrayList)
```

``` csharp
SetCurrentValuesForParameterField(customersByCityReport,
arrayList);
```

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.

  1. From the Build menu, select Build Solution.

  2. If you have any build errors, go ahead and fix them now.

  3. From the Debug menu, click Start.

The CustomersByCity report displays successfully, showing listings for customers in Paris and Tokyo.
  1. Return to Visual Studio and click Stop to exit from debug mode.