Listing 20-13 is a generic method for modifying a discrete parameter in a report. Pass it the parameter name, the parameter value, and the report object that you are working with.
If you remember from the description of Listing 20-1 which shows how to load and preview a report, prior to setting the ReportSource property you should call any code that performs runtime modification on the report object. This is where you would call this procedure.
Listing 20-13. Modifying a discrete parameter.
[VB.NET]
Public Shared Sub SetDiscreteParameter(ByVal ParameterName As String, _
ByVal NewValue As Object, ByVal rcd As CrystalDecisions.ReportAppServer.ClientDoc.ISCDReportClientDocument)
Dim OldParameter As CrystalDecisions.ReportAppServer.DataDefModel.ParameterField, NewParameter As _
CrystalDecisions.ReportAppServer.DataDefModel.ParameterField
Dim myParameterValue As CrystalDecisions.ReportAppServer.DataDefModel.ParameterFieldDiscreteValue
'Get a reference to the existing parameter
Dim ParameterIndex As Integer
ParameterIndex = rcd.DataDefinition.ParameterFields.Find(ParameterName, _
CrystalDecisions.ReportAppServer.DataDefModel.CrFieldDisplayNameTypeEnum.crFieldDisplayNameName, _
CrystalDecisions.ReportAppServer.DataDefModel.CeLocale.ceLocaleUserDefault)
OldParameter = DirectCast(rcd.DataDefinition.ParameterFields(ParameterIndex), _
CrystalDecisions.ReportAppServer.DataDefModel.ParameterField)
'Create the new parameter and base if off the existing parameter
NewParameter = New CrystalDecisions.ReportAppServer.DataDefModel.ParameterFieldClass()
OldParameter.CopyTo(NewParameter, True)
'Create the value object that will go in the parameter object
myParameterValue = New CrystalDecisions.ReportAppServer.DataDefModel.ParameterFieldDiscreteValueClass()
myParameterValue.Value = NewValue
'Save the parameter
NewParameter.CurrentValues.Add(myParameterValue)
rcd.DataDefController.ParameterFieldController.Modify(OldParameter, NewParameter)
End Sub
[C#]
public static void SetDiscreteParameter(string ParameterName, object NewValue, CrystalDecisions.ReportAppServer.ClientDoc.ISCDReportClientDocument rcd)
{
CrystalDecisions.ReportAppServer.DataDefModel.ParameterField OldParameter, NewParameter;
CrystalDecisions.ReportAppServer.DataDefModel.ParameterFieldDiscreteValue myParameterValue;
//Get a reference to the existing parameter
int ParameterIndex;
ParameterIndex = rcd.DataDefinition.ParameterFields.Find(ParameterName,
CrystalDecisions.ReportAppServer.DataDefModel.CrFieldDisplayNameTypeEnum.crFieldDisplayNameName, CrystalDecisions.ReportAppServer.DataDefModel.CeLocale.ceLocaleUserDefault);
OldParameter = (CrystalDecisions.ReportAppServer.DataDefModel.ParameterField)rcd.DataDefinition.ParameterFields[ParameterIndex];
//Create the new parameter and base if off the existing parameter
NewParameter = new CrystalDecisions.ReportAppServer.DataDefModel.ParameterFieldClass();
OldParameter.CopyTo(NewParameter, true);
//Create the value object that will go in the parameter object
myParameterValue = new CrystalDecisions.ReportAppServer.DataDefModel.ParameterFieldDiscreteValueClass();
myParameterValue.Value = NewValue;
//Save the parameter
NewParameter.CurrentValues.Add(myParameterValue);
rcd.DataDefController.ParameterFieldController.Modify(OldParameter, NewParameter);
}
Listing 20-14 shows a generic method for modifying a range parameter in a report. Pass it the parameter name, the beginning and ending values and the report object that you are working with. To make this example easier, it is assumed that each value uses inclusive bounds. It would be easy for you to modify it so that this is also passed as a parameter.
Listing 20-14. Modifying a range parameter.
[VB.NET]
Public Shared Sub SetRangeParameter(ByVal ParameterName As String, ByVal NewBeginValue As Object, ByVal NewEndValue As Object, _
ByVal rcd As CrystalDecisions.ReportAppServer.ClientDoc.ISCDReportClientDocument)
Dim OldParameter As CrystalDecisions.ReportAppServer.DataDefModel.ParameterField, NewParameter As _
CrystalDecisions.ReportAppServer.DataDefModel.ParameterField
Dim MyParameterValue As CrystalDecisions.ReportAppServer.DataDefModel.ParameterFieldRangeValue
'Get a reference to the existing parameter
Dim ParameterIndex As Integer
ParameterIndex = rcd.DataDefinition.ParameterFields.Find(ParameterName, _
CrystalDecisions.ReportAppServer.DataDefModel.CrFieldDisplayNameTypeEnum.crFieldDisplayNameName, _
CrystalDecisions.ReportAppServer.DataDefModel.CeLocale.ceLocaleUserDefault)
OldParameter = DirectCast(rcd.DataDefinition.ParameterFields(ParameterIndex), _
CrystalDecisions.ReportAppServer.DataDefModel.ParameterField)
'Create the new parameter and base if off the existing parameter
NewParameter = New CrystalDecisions.ReportAppServer.DataDefModel.ParameterFieldClass()
OldParameter.CopyTo(NewParameter, True)
'Create the value object that will go in the parameter object
MyParameterValue = New CrystalDecisions.ReportAppServer.DataDefModel.ParameterFieldRangeValueClass()
MyParameterValue.BeginValue = NewBeginValue
MyParameterValue.EndValue = NewEndValue
MyParameterValue.LowerBoundType = CrystalDecisions.ReportAppServer.DataDefModel.CrRangeValueBoundTypeEnum.crRangeValueBoundTypeInclusive
MyParameterValue.UpperBoundType = CrystalDecisions.ReportAppServer.DataDefModel.CrRangeValueBoundTypeEnum.crRangeValueBoundTypeInclusive
'Save the parameter
NewParameter.CurrentValues.Add(MyParameterValue)
rcd.DataDefController.ParameterFieldController.Modify(OldParameter, NewParameter)
End Sub
[C#]
public static void SetRangeParameter(string ParameterName, object NewBeginValue, object NewEndValue, CrystalDecisions.ReportAppServer.ClientDoc.ISCDReportClientDocument rcd)
{
CrystalDecisions.ReportAppServer.DataDefModel.ParameterField OldParameter, NewParameter;
CrystalDecisions.ReportAppServer.DataDefModel.ParameterFieldRangeValue MyParameterValue;
//Get a reference to the existing parameter
int ParameterIndex;
ParameterIndex = rcd.DataDefinition.ParameterFields.Find(ParameterName,
CrystalDecisions.ReportAppServer.DataDefModel.CrFieldDisplayNameTypeEnum.crFieldDisplayNameName,
CrystalDecisions.ReportAppServer.DataDefModel.CeLocale.ceLocaleUserDefault);
OldParameter = (CrystalDecisions.ReportAppServer.DataDefModel.ParameterField)rcd.DataDefinition.ParameterFields[ParameterIndex];
//Create the new parameter and base if off the existing parameter
NewParameter = new CrystalDecisions.ReportAppServer.DataDefModel.ParameterFieldClass();
OldParameter.CopyTo(NewParameter, true);
//Create the value object that will go in the parameter object
MyParameterValue = new CrystalDecisions.ReportAppServer.DataDefModel.ParameterFieldRangeValueClass();
MyParameterValue.BeginValue = NewBeginValue;
MyParameterValue.EndValue = NewEndValue;
MyParameterValue.LowerBoundType = CrystalDecisions.ReportAppServer.DataDefModel.CrRangeValueBoundTypeEnum.crRangeValueBoundTypeInclusive;
MyParameterValue.UpperBoundType = CrystalDecisions.ReportAppServer.DataDefModel.CrRangeValueBoundTypeEnum.crRangeValueBoundTypeInclusive;
//Save the parameter
NewParameter.CurrentValues.Add(MyParameterValue);
rcd.DataDefController.ParameterFieldController.Modify(OldParameter, NewParameter);
}