Crystal Reports Online Training

Learn Online, Anytime, Anywhere

Step-by-step online tutorials.

16.04 Parameter Data Types

Parameters have many different types of data that can be assigned to them. A parameter can hold a discrete value (a number, string, etc) or it can hold more complex values such as range values, multiple values, and a combination of discrete and range values. Assigning data to a parameter becomes increasingly complex as you exploit all of types of data it can hold. In the next few sections we’re going to learn how to assign the different data to a parameter. As the data becomes more complex, you will need to increase your understanding of the classes that manage parameters and see how Crystal Reports stores parameters. We’ll start off simple and build from there.

The code samples in the following sections all use the ReportDocument class. The CrystalReportsViewer class has different methods and properties than the ReportDocument class and this code will not work with it. Modifying parameters using the CrystalReportsViewer class is discussed later in this chapter.

Discrete Values

Discrete values consist of a single value that is a simple data type. It can be a number, string date or Boolean. To assign it to a parameter, you only need the parameter name and the new value. The first example assigns a string value to the parameter and the second example assigns todays date to the parameter.

Listing 16-4. Assign discrete values to parameters
[VB.NET]
myReport.SetParameterValue("Country", "USA")
myReport.SetParameterValue("PrintDate", DateTime.Now())
[C#]
myReport.SetParameterValue("Country", "USA");
myReport.SetParameterValue("PrintDate", DateTime.Now());

Range Values

Assigning range values to a parameter is a little more complicated because there isn’t a compatible data type in the .NET syntax. Instead, you have to declare and instantiate an object variable using the ParameterRangeValue class. When using this class, there are two properties used for setting the range: StartValue and EndValue.

Listing 16-5. Assign range values to parameters
[VB.NET]
Dim myParameterName as String = "IdRange"
Dim myParameterRangeValue As New CrystalDecisions.Shared.ParameterRangeValue()
'Assign the start and end values to the parameter object
myParameterRangeValue.StartValue = 1000
myParameterRangeValue.EndValue = 1999
'Reference the actual parameter stored in the report's ParameterFields collection
myReport.SetParameterValue(myParameterName, myParameterRangeValue)
[C#]
string myParameterName = "IdRange";
CrystalDecisions.Shared.ParameterRangeValue myParameterRangeValue = new CrystalDecisions.Shared.ParameterRangeValue();
//Assign the start and end values to the parameter object
myParameterRangeValue.StartValue = 1000;
myParameterRangeValue.EndValue = 1999;
'Reference the actual parameter stored in the report's ParameterFields collection
myReport.SetParameterValue(myParameterName, myParameterRangeValue);

This listing first declares two variables: one for storing the parameter name and one for storing the range value. The next two lines assign the start and end points for the range value. Both of these properties are declared as an Object and you can pass them any simple data type. Lastly, this code uses the SetParameterValue() method to assign the range value to the parameter. Once it’s finished, you can preview the report and it will have the new parameter value.

Multiple Values

You can pass multiple values to a parameter using an array. Simply create and populate the array and pass it to the SetParameterValue() method.

Listing 16-6. Assign multiple values to a parameter
[VB.NET]
Dim InvoiceDates(1) As Date
InvoiceDates(0) = Date.Parse("2/1/2001")
InvoiceDates(1) = Date.Parse("2/5/2001")
'Assign the array to the parameter value
myReport.SetParameterValue("InvoiceDatesParam", InvoiceDates)
[C#]
DateTime[] InvoiceDates = new DateTime[2];
InvoiceDates[0] = DateTime.Parse("2/1/2001");
InvoiceDates[1] = DateTime.Parse("2/5/2001");
//Assign the array to the parameter value
myReport.SetParameterValue("InvoiceDatesParam", InvoiceDates);

This listing creates an array that stores two dates. This array is passed to report using the SetParameterValue() method prior to previewing the report.

Passing an array is very powerful because the array can consist of simple data types, the ParameterRangeValue class, or a combination of the two. For example, if you wanted to pass multiple range values to the parameter, declare the array using the ParameterRangeValue class and then use the code in listing 16-5 to instantiate and assign data to each range value.

Crystal Reports also lets you combine multiple discrete and range values in a single parameter. To populate this type of parameter in your code, declare the array using the Object class and then assign whatever combination of data you want to it. For example, you could assign date constants to some of the elements in the array and assign ParameterRangeValue objects to the other elements (assuming that each range also uses the date data type). You need to be careful that the array doesn’t accidentally have different data types. For example, you can’t store a number in some of the elements and string in the others.