Crystal Reports Online Training

Learn Online, Anytime, Anywhere

Step-by-step online tutorials.

16.10 Simplifying Parameters

Simplifying Parameters

If you found that programming parameters was difficult to understand, you are not alone. There are a lot of steps to learn and it doesn’t help that the ReportDocument object is programmed differently than the viewer control. To try and simplify this, I wrote some generic methods that can be added to your reporting library and be used by many projects. These methods modify discrete and range parameters for both the ReportDocument object and the viewer control. If you want the fastest way to modify parameters with no bugs, just copy this code into your own project and see how much easier it is to start using parameters.

All the methods are written so that you can pass a subreport name as the last parameter. To reference parameters on the main report, pass the empty string as the subreport name. Remember that ParameterFields collection is case sensitive for parameter names. This applies to the subreport names as well.

This code is just a variation on what you’ve already seen. To understand how it works see the previous sections in this chapter. This code only works for the CurrentValues collection. As a .NET developer it is unlikely you will do much programming with the DefaultValues collection. If you want to use this code for updating the DefaultValues collection it is easy enough to modify it on your own.

Listing 16-4 and 16-5 modify parameters using the report object. They modify discrete values and range values. Pass each method the report object, the parameter field’s name and any information necessary to fill the parameter’s value. Notice that in Listing 16-5 the method definition for updating a range parameter is quite large. This is because a range parameter has many properties that need to be set.

Listing 16-4. Modify a discrete parameter using the report object.
Public Sub SetParameterDiscreteWithReport(ByVal MyReport As CrystalDecisions.CrystalReports.Engine.ReportDocument, ByVal ParameterName As String, ByVal Value As Object, ByVal SubreportName as String)
Dim MyParameterFieldDefinition As _
CrystalDecisions.CrystalReports.Engine.ParameterFieldDefinition
Dim MyParameterValues As CrystalDecisions.Shared.ParameterValues
Dim MyParameterDiscreteValue As _
CrystalDecisions.Shared.ParameterDiscreteValue
MyParameterFieldDefinition = _
MyReport.DataDefinition.ParameterFields.Item(ParameterName, SubreportName)
MyParameterValues = New CrystalDecisions.Shared.ParameterValues
MyParameterDiscreteValue = _
New CrystalDecisions.Shared.ParameterDiscreteValue
MyParameterDiscreteValue.Value = Value
MyParameterValues.Add(MyParameterDiscreteValue)
MyParameterFieldDefinition.ApplyCurrentValues(MyParameterValues)
End Sub
Listing 16-5. Modify a range parameter using the report object.
Public Shared Sub SetParameterRangeWithReport(ByVal MyReport As CrystalDecisions.CrystalReports.Engine.ReportDocument, ByVal ParameterName As String, ByVal StartValue As Object, ByVal EndValue As Object, ByVal LowerBoundType As CrystalDecisions.Shared.RangeBoundType, ByVal UpperBoundType As CrystalDecisions.Shared.RangeBoundType, ByVal SubreportName as String)
Dim MyParameterFieldDefinition As _
CrystalDecisions.CrystalReports.Engine.ParameterFieldDefinition
Dim MyParameterValues As CrystalDecisions.Shared.ParameterValues
Dim MyParameterRangeValue As _
CrystalDecisions.Shared.ParameterRangeValue
MyParameterFieldDefinition = _
MyReport.DataDefinition.ParameterFields.Item(ParameterName, SubreportName)
MyParameterValues = New CrystalDecisions.Shared.ParameterValues
MyParameterRangeValue = _
New CrystalDecisions.Shared.ParameterRangeValue
MyParameterRangeValue.StartValue = StartValue
MyParameterRangeValue.EndValue = EndValue
MyParameterRangeValue.LowerBoundType = LowerBoundType
MyParameterRangeValue.UpperBoundType = UpperBoundType
MyParameterValues.Add(MyParameterRangeValue)
MyParameterFieldDefinition.ApplyCurrentValues(MyParameterValues)
End Sub

Listing 16-6 and 16-7 modify parameters using the viewer control. Pass it the viewer control, the parameter name, and any information necessary to fill the parameter’s value. Before calling this method you have to instantiate the report object and assign it to the viewer’s ReportSource property.

Listing 16-6. Modify a discrete parameter using the viewer control.
Public Sub SetParameterDiscreteWithViewer(ByVal CrystalReportViewer1 As CrystalDecisions.Windows.Forms.CrystalReportViewer, ByVal ParameterName As String, ByVal ParameterValue As Object, ByVal SubreportName as String)
Dim MyParameterFields As CrystalDecisions.Shared.ParameterFields
Dim MyParameterField As CrystalDecisions.Shared.ParameterField
Dim MyParameterDiscreteValue As CrystalDecisions.Shared.ParameterDiscreteValue
MyParameterFields = CrystalReportViewer1.ParameterFieldInfo
MyParameterField = MyParameterFields(ParameterName, SubreportName)
MyParameterDiscreteValue = New CrystalDecisions.Shared.ParameterDiscreteValue
MyParameterDiscreteValue.Value = ParameterValue
MyParameterField.CurrentValues.Add(MyParameterDiscreteValue)
CrystalReportViewer1.ParameterFieldInfo = MyParameterFields
End Sub
Listing 16-7. Modify a range parameter using the viewer control.
Public Sub SetParameterRangeWithViewer(ByVal CrystalReportViewer1 As CrystalDecisions.Windows.Forms.CrystalReportViewer, ByVal ParameterName As String, ByVal StartValue As Object, ByVal EndValue As Object, ByVal LowerBoundType As CrystalDecisions.Shared.RangeBoundType, ByVal UpperBoundType As CrystalDecisions.Shared.RangeBoundType, ByVal SubreportName as String)
Dim MyParameterFields As CrystalDecisions.Shared.ParameterFields
Dim MyParameterField As CrystalDecisions.Shared.ParameterField
Dim MyParameterRangeValue As CrystalDecisions.Shared.ParameterRangeValue
MyParameterFields = CrystalReportViewer1.ParameterFieldInfo
MyParameterField = MyParameterFields(ParameterName, SubreportName)
MyParameterRangeValue = New CrystalDecisions.Shared.ParameterRangeValue
MyParameterRangeValue.StartValue = StartValue
MyParameterRangeValue.EndValue = EndValue
MyParameterRangeValue.LowerBoundType = LowerBoundType
MyParameterRangeValue.UpperBoundType = UpperBoundType
MyParameterField.CurrentValues.Add(MyParameterRangeValue)
End Sub

Listing 16-8 and 16-9 show how to use the above methods in an application. To keep the examples simple, they pass the empty string as the subreport name. If a report has subreports with parameters, you should pass the subreport name instead. Notice that these example uses date parameters. I did this because many people have trouble working with dates and this shows how easy it is to do.

Listing 16-8 demonstrates using the report object to update parameters. It instantiates a new report object and set the values for a discrete and range parameter.

Listing 16-8. Modify parameters using the report object.
Dim MyReport As New CrystalReport1
SetParameterDiscreteWithReport(MyReport, "CustomerId", "DotNetTech", "")
SetParameterRangeWithReport(MyReport, "DateRange", #2/1/1997#, #2/15/1997#, _
CrystalDecisions.[Shared].RangeBoundType.BoundInclusive, _
CrystalDecisions.[Shared].RangeBoundType.BoundExclusive, "")
CrystalReportViewer1.ReportSource = MyReport

Listing 16-9 demonstrates using the viewer to update parameters. It instantiates a new report object and assigns it to the viewer. Then it calls the methods to update the parameters.

Listing 16-9 Modify parameters using the viewer control.
Dim MyReport As New CrystalReport1
CrystalReportViewer1.ReportSource = MyReport
SetParameterDiscreteWithViewer(CrystalReportViewer1, "MyBirthday", #5/23/1968#, "")
SetParameterRangeWithViewer(CrystalReportViewer1, "DateRange", #2/1/1997#, _
#2/15/1997#, CrystalDecisions.[Shared].RangeBoundType.BoundInclusive, _
CrystalDecisions.[Shared].RangeBoundType.BoundExclusive, "")