Modifying the Current Values in a Parameter
A problem with parameters is that the user interface for prompting the user to enter a value isn’t very exciting. Crystal Reports uses its own dialog box to prompt the user and it is generic and bland. Fortunately, Crystal Reports .NET lets you override a parameter’s default behavior. When opening a report with parameters, you can use a form in your project that prompts the user for all the relevant data and pass this directly to the report. Or you could use business logic to determine the data passed to the parameter. This gives your reports a consistent interface with your application so that the user feels that the two are truly integrated. You also get the benefit of validating user input prior to printing the report. This reduces the chances of an error occurring when running reports.
Parameters have two different ways to classify values: Current and Default. Current values are the data that the user types into the Enter Parameters dialog box when printing the report. Default values are the data that is presented to the user in a list format so that he can pick which data he wants the report filtered on. Default values are usually set when the report is being designed and don’t change very often. The following sections discuss the code necessary to set a parameter’s current values. The code for overriding the default values is discussed later in this chapter.
To modify a report parameter, call the SetParameterValue() method of the ReportDocument class. Pass it the parameter name and the new parameter value.
myReport.SetParameterValue("Country", "USA")
The SetParameterValue() method also lets you pass it a subreport name if you need to modify a parameter that is in a subreport.
myReport.SetParameterValue("Country", "USA", "Subreport1")
When referring to a parameter name in your .NET application, don’t put a ? in front of the name. Although this is required when referencing parameters from within the report, it isn’t used by .NET when referencing report objects via the object model. You’ll get the error Invalid Index if you do so.
The SetParameterValue() method is very flexible. It accepts any data type and assigns it to the parameter (assuming you use a compatible data type). Unlike the previous section on overriding formulas, you don’t have to convert the data to its string equivalent or worry about surrounding string data with quotes.
Since the SetParameterValue() method is so easy to use, it does have it’s limitations. They are listed below.
- Can’t modify a parameter’s default values list.
- Can’t be used to set parameters with the ASP.NET or Windows report viewer classes.
- Can’t modify any properties of the parameter object model.
Luckily, these are not significant limitations because the majority of the work you do with parameters won’t be affected. I would say that for most people, the SetParameterValue() method is all that you need to learn. If you find that these limitations don’t let you perform the work you need to do, a more thorough discussion of how to use the parameter classes to do advanced customization is discussed later in the following section.