Then in the Page_Init event of your form, add a single line of code to call the DataBind method immediately after the InitializeComponent() call, as shown here:


If you prefer to set the initial report source using the property pages, you will need to open the Properties page for the Report Viewer, and then select the (DataBindings) property, as we did in the first example. Then right-click the project name, and click Set As Startup Project. You're now ready to start setting working with parameters.

Again, a good starting place for working with parameters is looking at the different types of parameters you could have in a report; in addition to a parameter's data type (String, Number, and so on), you can also set options for how the parameter is entered. These options will seem familiar to you if you have already read the previous chapter, but they are repeated here just to drive the point home and for the benefit of anyone who doesn't want to flip back and find that particular section. There are three basic options available when creating parameter fields:

□ Discrete parameters accept single, discrete values; depending on how the parameter field was set up, it can accept only one value or multiple values.

□ Ranged parameters can accept a lower and upper value, selecting everything that is within that range of numbers, letters, and so on.

□ Discrete and Ranged parameters support a combination of the two different types, where you can enter multiple discrete values as well as multiple ranges.

Parameters within a report are contained within a collection called ParameterFields and within the collection, a set of attributes is stored for each field using the Parameter Field object. As we walk through this example, we will set properties relating to this object in this collection and then use this collection to set the ParameterFieldInfo property of the viewer, as shown subsequently:

crystalReportViewer1.ParameterFieldInfo = myParameterFields

To set the values for a particular parameter, we first need to dimension both the collection and object. We also need to create a variable to hold the value that we want to pass to the report for this parameter.

Because in this example we are passing only a discrete value to the parameter, we would simply create a variable by dimensioning it as a ParameterDiscreteValue. All we need to do at that point is set the value of the parameter equal to some value we have entered or hard-coded and then set the ParameterFieldInfo property of the Report Viewer.

Because we want to enter the value ourselves, we would want to put a textbox and a command button on another form to collect and submit the value to be used in our parameter to the form with the viewer present. To create a new form, select Project ^ Add Web Form and then add a textbox, label, and command button, which would make the form look something like Figure 5-15.

Figure 5-15

Set the properties of the textbox to make it blank when the form appears and change the name of the command button to "View Report." Then put the code behind to use Response.Redirect to go to the form that is hosting your Crystal Viewer and pass the value entered in the textbox as a URL parameter.

You can now go to the page where your viewer resides and use the following code to set your parameter fields. In this example, we are using a variable called "PassedlnValue" that contains the value passed from the previous page.

Dim myParameterFields As New ParameterFields()

Dim myParameterField As New ParameterField()

Dim myDiscreteValue As New ParameterDiscreteValue()

myParameterField.ParameterFieldName = "OriginCountry" myDiscreteValue.Value = PassedlnValue myParameterField.CurrentValues.Add(myDiscreteValue)


crystalReportViewerl.ParameterFieldlnfo = myParameterFields crystalReportViewerl.RefreshReport

The next scenario we are going to look at is for multiple discrete variables; in this situation we would use the same methods, only instead of setting a single value using the ParameterDiscreteValue variable, we would continually redeclare this variable and use it to push values into the ParameterField object. Again, the same method would apply here—creating a front-end page to collect the parameter information and then passing it to the page with the viewer on it. This would go behind the viewer page and again you would have to pass the values from the previous page. Here we have hard-coded them to make this code a bit easier to follow.

Dim myParameterFields As New ParameterFields()

Dim myParameterField As New ParameterField()

Dim myDiscreteValue As New ParameterDiscreteValue()

myParameterField.ParameterFieldName = "OriginCountry"

myDiscreteValue.Value = "USA"

myParameterField.CurrentValues.Add(myDiscreteValue) myDiscreteValue= New ParameterDiscreteValue() myDiscreteValue.Value = "Canada"

myParameterField.CurrentValues.Add(myDiscreteValue) myParameterFields.Add(myParameterField)

crystalReportViewerl.ParameterFieldlnfo = myParameterFields

In this example, we have used the variable twice to push both "USA" and "Canada" to the report. Remember, in your actual report design you must specify the parameter option—for example, whether it is a discrete, multi-value discrete, or ranged parameter. Otherwise you will receive an error message when running your report.

Finally, our last type of parameter is a ranged parameter, which can be set using the same method except instead of a discrete value, we are going to pass in a range, with a start and end value, as shown in the following code:

Dim myParameterFields As New ParameterFields() Dim myParameterField As New ParameterField() Dim myDiscreteValue As New ParameterDiscreteValue() Dim myRangeValues as New ParameterRangeValues()

myParameterField = New ParameterField() myParameterField.ParameterFieldName = "LastYearsSales"

myRangeValues.StartValue = 100000 myRangeValues.EndValue = 500000

myParameterField.CurrentValues.Add(myRangeValue) myParameterFields.Add(myParameterField)

You can also combine the two methods, depending on the type and number of parameters you have created in your report. You could have two parameters, for example, that accept discrete values, one that accepts a range, and still another that accepts a combination of the two. The methods used to set the values for these parameters are the same, so users can easily enter parameter values through your own application and filter the report content.

Was this article helpful?

0 0

Post a comment