Search code examples
vb.netparameterscrystal-reportscrystal-reports-xi

How to pass values to two different parameters


I am developing a Windows Application and use a Crystal Report in it.

I am able to pass value to one parameter of Crystal Report. Now, I have added one more parameter to Crystal Report, but confused about what modification I have to make in below code.

Below code is written to pass value to one parameter of CR.

Private Sub btnLoadBatch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadBatch.Click
    Try
        Dim cr As New crMonthwiseBatch
        Dim oBatches As New Batches

        Dim Month As Integer = dtFrom.Value.Date.Month
        Dim StartDateForBatch As Date = New DateTime(dtBatchStartFromMonth.Value.Year, dtBatchStartFromMonth.Value.Month, "1")
        Dim DaysinMonths As Integer = System.DateTime.DaysInMonth(dtBatchStartFromMonth.Value.Year, dtBatchStartFromMonth.Value.Month)
        Dim EndDateForBatch = StartDateForBatch.AddDays(DaysinMonths)

        oBatches.LoadByQuery("CreatedDate >= #" + StartDateForBatch + "# and CreatedDate <= #" + EndDateForBatch + "#")
        cr.SetDataSource(oBatches)

        Dim crParameterFieldDefinitions As ParameterFieldDefinitions
        Dim crParameterFieldDefinition As ParameterFieldDefinition
        Dim crParameterValues As New ParameterValues
        Dim crParameterDiscreteValue As New ParameterDiscreteValue

        crParameterDiscreteValue.Value = "Batch List of Month - " + MonthName(dtBatchStartFromMonth.Value.Month) + " " + dtBatchStartFromMonth.Value.Year.ToString
        crParameterFieldDefinitions = cr.DataDefinition.ParameterFields
        crParameterFieldDefinition = crParameterFieldDefinitions.Item("MonthName")
        crParameterValues = crParameterFieldDefinition.CurrentValues

        crParameterValues.Clear()
        crParameterValues.Add(crParameterDiscreteValue)
        crParameterFieldDefinition.ApplyCurrentValues(crParameterValues)

        CrystalReportViewerMonthwiseBatch.ReportSource = cr
        CrystalReportViewerMonthwiseBatch.Refresh()
    Catch ex As Exception

    End Try
End Sub

Solution

  • You can achieve it this way (look for comments above statement(s) for description)

    Approach 1:

    '...
    'collection of objects for every parameter field
    Dim crParameterFieldDefinitions As ParameterFieldDefinitions
    'represent a parameter field
    Dim crParameterFieldDefinition As ParameterFieldDefinition
    'collection of ParameterValue objects for every parameter field
    Dim crParameterValues As New ParameterValues()
    'for retrieving and setting discrete value parameters
    Dim crParameterDiscreteValue As New ParameterDiscreteValue()
    
    'get parameters collection in current crystal report
    crParameterFieldDefinitions = cr.DataDefinition.ParameterFields
    
    'adding FIRST parameter
    'get the parameter in the current report and assign a value
    crParameterFieldDefinition = crParameterFieldDefinitions("PARAM_1_NAME")
    crParameterDiscreteValue.Value = "PARAM_1_VALUE"
    crParameterValues = crParameterFieldDefinition.CurrentValues
    
    'clear old/default values assigned to current parameter, add and apply new value
    crParameterValues.Clear()
    crParameterValues.Add(crParameterDiscreteValue)
    crParameterFieldDefinition.ApplyCurrentValues(crParameterValues)
    'finished adding FIRST parameter
    
    'adding SECOND and subsequent parameters
    'reset the collections
    crParameterDiscreteValue = New ParameterDiscreteValue()
    crParameterValues = New ParameterValues()
    
    'get parameter, assign values, clear old values, add to collection and apply
    crParameterFieldDefinition = crParameterFieldDefinitions("PARAM_2_NAME")
    crParameterDiscreteValue.Value = "PARAM_2_VALUE"
    crParameterValues = crParameterFieldDefinition.CurrentValues
    crParameterValues.Clear()
    crParameterValues.Add(crParameterDiscreteValue)
    crParameterFieldDefinition.ApplyCurrentValues(crParameterValues)
    'finished adding SECOND parameter
    '...
    'Just display the report
    

    Approach 2:
    If you have multiple parameters it can also be achieved by using a separate procedure such as

    Private Sub SetParams(crRpt As CrystalDecisions.CrystalReports.Engine.ReportDocument, strParamName As String, strParamValue As String)
       For Each pfField As CrystalDecisions.CrystalReports.Engine.ParameterFieldDefinition In crRpt.DataDefinition.ParameterFields
          If pfField.Name.ToString().ToLower() = strParamName.ToLower() Then
             Try
                Dim crParameterValues As New CrystalDecisions.Shared.ParameterValues()
                Dim crParameterDiscreteValue As New CrystalDecisions.Shared.ParameterDiscreteValue()
                crParameterDiscreteValue.Value = strParamValue
                crParameterValues = pfField.CurrentValues
                crParameterValues.Clear()
                crParameterValues.Add(crParameterDiscreteValue)
                pfField.ApplyCurrentValues(crParameterValues)
             Catch ex As Exception
                'add your exception handling mechanism here
                MessageBox.Show(ex.Message)
             End Try
          End If
       Next
    End Sub
    

    The above routine can be called for any report object (as cr in example below) to add any number of parameters to the reports

    SetParams(cr, "@Param_1_Name", "Param_1_Value")
    SetParams(cr, "@Param_2_Name", "Param_2_Value")
    '...
    SetParams(cr, "@Param_n_Name", "Param_n_Value")