ARTICLE

Tip: How to pass Crystal Report Parameters Programmatically?

Posted by Mahesh Chand Articles | Crystal Reports C# November 02, 2006
This tip shows how to pass a Crystal Report parameters and their values programmatically.
Reader Level:

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.

 

' 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

 

Article Extensions
Contents added by dili on Sep 11, 2013
When  I used the coding you provided I got a error as this "Invalid index. (Exception from HRESULT: 0x8002000B (DISP_E_BADINDEX))"
I used the coding to a button.
Coding I used is this
 Dim cryRpt As New ReportDocument
        Dim report As New CrystalReport1

        ' 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(TextBox1.Text)

        crParameterValues = crParameterFieldLocation.CurrentValues

        crParameterDiscreteValue = New CrystalDecisions.Shared.ParameterDiscreteValue

        crParameterDiscreteValue.Value = TextBox1.Text

        crParameterValues.Add(crParameterDiscreteValue)

        crParameterFieldLocation.ApplyCurrentValues(crParameterValues)

        ' Add a parameter value - END
        report.SetDataSource(ds)

        ' Set CrystalReportViewer.ReportSource


        CrystalReportViewer1.ReportSource = report

             cryRpt.Load("G:\trial\CrystalReport1.rpt")
        CrystalReportViewer1.ReportSource = cryRpt
        CrystalReportViewer1.Refresh()
COMMENT USING
Employers - Post Free Jobs