Author Topic: How to pass Crystal Report Parameters Programmatically?  (Read 6998 times)

Offline admin

  • Administrator
  • Sr. Member
  • *****
  • Posts: 296
    • View Profile
How to pass Crystal Report Parameters Programmatically?
« on: November 21, 2009, 12:03:29 PM »
You can pass parameter values to a crystal report programmatically using ReportDocument.DataDefinition.ParameterFields member, which represents a collection of parameters associated with a report.

 

Before you use ParameterFields, you must import CrystalReport.Engine namespace by adding the following line to your code:

 

Imports CrystalDecisions.CrystalReports.Engine

 

In my report, I have two parameters called ParameterName1 and ParameterName2 and I want to pass values as "Parameter1Value" and "Parameter2Value" respectively. If you have more than 2 parameters, just repeat the same steps between "START" and "END" commented lines. Also read the comments in the code carefully.

 
Code: [Select]

' Create report instance. This is the class added to your project

' when you added the report to the project

Dim report As MyReport = New MyReport

 

' Fill data in DataSet here. Skip this step if your report is calling

' a stored procedure direct

Dim ds As DataSet = New DataSet

' ds = GetDataFromDatabase()

 

Dim crParameterDiscreteValue As ParameterDiscreteValue

Dim crParameterFieldDefinitions As ParameterFieldDefinitions

Dim crParameterFieldLocation As ParameterFieldDefinition

Dim crParameterValues As ParameterValues

       

'

' Get the report parameters collection.

'

crParameterFieldDefinitions = report.DataDefinition.ParameterFields

 

' Add a parameter value - START

crParameterFieldLocation = crParameterFieldDefinitions.Item("@ParameterName1")

crParameterValues = crParameterFieldLocation.CurrentValues

crParameterDiscreteValue = New CrystalDecisions.Shared.ParameterDiscreteValue

crParameterDiscreteValue.Value = "Parameter1Value"

crParameterValues.Add(crParameterDiscreteValue)

crParameterFieldLocation.ApplyCurrentValues(crParameterValues)

' Add a parameter value - END

 

crParameterFieldLocation = crParameterFieldDefinitions.Item("@ParameterName2")

crParameterValues = crParameterFieldLocation.CurrentValues

crParameterDiscreteValue = New CrystalDecisions.Shared.ParameterDiscreteValue

crParameterDiscreteValue.Value = "Parameter2Value"

crParameterValues.Add(crParameterDiscreteValue)

crParameterFieldLocation.ApplyCurrentValues(crParameterValues)

 

'

' Set report's DataSource. Skip this step if your report is calling a
' stored procedure direct in the report.

'

report.SetDataSource(ds)

 

'

' Set CrystalReportViewer.ReportSource

'

 

CrystalReportViewer1.ReportSource = report