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

 

Login to add your contents and source code to this article
post comment
     

Jesse, technically code is same. All you have to do is, convert it to C#.

Posted by Mahesh Chand Feb 13, 2013

Thanks, but I'm looking for help in C#! This VB example is posted under Crystal Reports C# on c-sharpcorner.com

Posted by Jesse Feb 08, 2013

Hello Mahesh, Thanks, it helped. I can now see the value i passed into the report. Now, I have a problem when using this in the 'Add command' when I call a query like: Select *from categories where categoryid={?@catid} => error: No value given for one or more required parameters Select * from categories where categoryid IN ({?@catid}) => error: No value given for one or more required parameters Select *from categories where categoryid='{?@catid}' => No results returned Can you tell me how to use this parameter from the 'Add Command' module? Thanks and regards, Norman

Posted by norman_uy Feb 16, 2011

i am trying to send parameter by the user and to preview the report. for example, the user will select the invoice no and click the preview button to view the report. i it working when i run the project form the visual studio 2008 . but when i publish it and try to work from the browser (opera / firefox ) also shared the shared the folder "C:\Documents and Settings\Administrator\Local Settings\Temp" and "C:\WINDOWS\Temp" it is not working and show the following error: Error in File C:\WINDOWS\TEMP\rptLedgerSubsidiary {1949386A-245F-4BE8-8354-62CA224759CE}.rpt: Access to report file denied. Another program may be using it.

Posted by mazez sha Feb 01, 2011

hello, i have to filter the data which is using dropdownlist and 2 textboxs (3 of 'em autopostback). i'm done display the data in gridview and the problem is i need to display same gridview data in crystal report to another page when i click a button view. actually i'm new in .net and i have no idea how to do it.. i'm using c# .net 2.0. thanks in advance.

Posted by rebecca evans Dec 13, 2010
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Get Career Advice from Experts