Share via


Adding the Subreport Parameter Code

You are now ready to add the parameter code for the subreport to the code-behind class. To begin, you create a private helper method, SetDateRangeForOrders().

  1. Open the Web or Windows Form.

  2. From the View menu, click Code.

  3. At the top of the class, add two new constants below the existing PARAMETER_FIELD_NAME constant added during the previous tutorial.

    Private Const SUBREPORT_PARAMETER_FIELD_NAME As String =
    "OrderDateRange"
    Private Const SUBREPORT_NAME As String = "CustomerOrders"
    
    private const string SUBREPORT_PARAMETER_FIELD_NAME =
    "OrderDateRange";
    private const string SUBREPORT_NAME = "CustomerOrders";
    
  4. At the bottom of the class, create a new private method named SetDateRangeForOrders() with three parameters: ParameterFields, a string startDate, and a string endDate.

    Private Sub SetDateRangeForOrders(ByVal myParameterFields As
    ParameterFields, ByVal startDate As String, ByVal endDate As
    String)
    
    End Sub
    
    private void SetDateRangeForOrders(ParameterFields parameterFields,
    string startDate, string endDate)
    {
    }
    
  5. Within this method, declare and instantiate the ParameterRangeValue class.

> [!NOTE]
> <P>For the ParameterRangeValue class to be accessible, you must include an "Imports" [Visual Basic] or "using" [C#] statement 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 myParameterRangeValue As ParameterRangeValue = New
ParameterRangeValue()
```

``` csharp
ParameterRangeValue parameterRangeValue = new
ParameterRangeValue();
```
  1. Set the StartValue property of the ParameterRangeValue instance to the startDate method parameter.
> [!NOTE]
> <P>The StartValue and EndValue properties of the ParameterRangeValue class accept values of type Object. This generic type allows the range value that is passed in to be of many types, including: text, number, date, currency, or time.</P>


``` vb
myParameterRangeValue.StartValue = startDate
```

``` csharp
parameterRangeValue.StartValue = startDate;
```
  1. Set the EndValue property of the ParameterRangeValue instance to the endDate method parameter.

    myParameterRangeValue.EndValue = endDate
    
    parameterRangeValue.EndValue = endDate;
    
  2. Set the lower and upper boundaries to be bound-inclusive.

> [!NOTE]
> <P>For BoundInclusive, the upper and lower range values are included in the range.</P>


``` vb
myParameterRangeValue.LowerBoundType =
RangeBoundType.BoundInclusive
myParameterRangeValue.UpperBoundType =
RangeBoundType.BoundInclusive
```

``` csharp
parameterRangeValue.LowerBoundType = RangeBoundType.BoundInclusive;
parameterRangeValue.UpperBoundType = RangeBoundType.BoundInclusive;
```

You are now ready to assign the ParameterRangeValue instance to the parameter of the subreport.
  1. Retrieve the ParameterField instance from the ParameterFields indexed class, which is based on two indexed values: the subreport parameter field name and the subreport name. Pass in the two constant values that you declared at the top of the class.

    Dim myParameterField As ParameterField =
    myParameterFields(SUBREPORT_PARAMETER_FIELD_NAME, SUBREPORT_NAME)
    
    ParameterField parameterField =
    parameterFields[SUBREPORT_PARAMETER_FIELD_NAME, SUBREPORT_NAME];
    
  2. Call the Clear() method of the CurrentValues property of the ParameterField instance to remove any existing values from the CurrentValues property.

    myParameterField.CurrentValues.Clear()
    
    parameterField.CurrentValues.Clear();
    
  3. Add the ParameterRangeValue instance, which you created earlier, to the CurrentValues property of the ParameterField instance.

    myParameterField.CurrentValues.Add(myParameterRangeValue)
    
    parameterField.CurrentValues.Add(parameterRangeValue);
    

This step procedure has set start and end date values into a ParameterRangeValue instance and passed those values to the OrderDateRange parameter in the CustomerOrders subreport.