How to dynamically pass parameters from a form to crystal reports at runtime in

Many a times, we want to pass some values, say value of a label or a textbox to a crystal report query at runtime. Here I will discuss the steps to do the same. Parameters are added to the report query with a preceding '@' symbol and initialized to their default values. And then, they are made to hold the values from the objects like textbox or label using the code mentioned in this article.

Suppose we have a label, textbox and button named as label1, textbox1 and button1 respectively in a application form. On click of the button , we display a crystal report which is connected to the database . The report has the following sql query -

"select * from table_name where field1 = "abc" and field2 = "def"

where table_name is the name of the table and field1 and field2 are two of it's field names.

But at runtime, we want field1 to take the value of label1 and field2 to that of textbox1.

Here I will discuss the steps to pass these parameters to the report dynamically at runtime

Step1: Include these namespaces in the form where you have your crystalreportviewer control

Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared

Step2: Add two parameters to the report @txt1 and @txt2 to the report and change the query to "select * from table_name where field1 = {?@txt1} and field2 = {?@txt2}"

Step3: come back to the form where you have your crystalreportviewer control and start writing these lines of code

Dim ParamFields As ParameterFields = Me.CrystalReportViewer1.ParameterFieldInfo

'declare two parameterfields

Dim P_txt1 As New ParameterField 'to hold the textbox value
Dim P_txt2 As New ParameterField 'to hold the label value

'setting the variables to the ones declared in the report query
P_txt1.Name = "@txt1"
P_txt2.Name = "@txt2"

Dim P_txt1_Value As New ParameterDiscreteValue
Dim P_txt2_Value As New ParameterDiscreteValue

'Form1 is the name of the form that holds the lablel and button
P_txt1_Value.Value = Form1.label1.text
P_txt2_Value.Value = Form1.textbox1.text



After implementing the code, the report will show the values from the label and textbox at runtime.

Pratibha Mohanty


Guest Author: Argha Chakraborty23 Oct 2012

After doing this it is showing "Wrong parameter Value". What to do?

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: