Click here to Skip to main content
14,981,116 members
Articles / Programming Languages / Visual Basic
Article
Posted 10 Jan 2005

Stats

384.4K views
5.7K downloads
71 bookmarked

How to pass discrete parameters to Crystal Reports

Rate me:
Please Sign up or sign in to vote.
3.93/5 (22 votes)
10 Jan 2005CPOL1 min read
This is a simple and small user control that will help programmers to pass discrete parameters to Crystal Reports without writing lengthy code.

Introduction

This is a simple and small user control that will help programmers to pass discrete parameters to Crystal Reports without writing lengthy code.

In this control, we are having two properties and one method:

  • ReportFileName

    This is a String parameter. This is to specify the report filename.

  • ParameterFields

    This is a String array parameter. This is to specify the parameters.

  • CrySet

    This method/function which will pass the corresponding parameter values to the report.

The Code

Before discussing the technical stuff, first we will see the pre-requirements. We need to add the following .NET references:

  • Crystaldecisions.crystalreports.engine
  • Crystaldecisions.reportsource
  • Crystaldecisions.shared
  • Crystaldecisions.windows.forms

Declaration

VB
Dim pFields As New ParameterFields()
Dim pField As New ParameterField()
Dim disVal As New ParameterDiscreteValue()
Dim rVal As New ParameterRangeValue()

Assigning the Parameter Name and its Values

VB
' parameter filed, must match with parameter in the report
pField.ParameterFieldName = "MyName"

disVal.Value = "CodeProject"     'Set the discrete Value
pField.CurrentValues.Add(disVal) 'Pass it to the parameter

With the example below, we can pass the range value:

VB
rVal.StartValue = 150
rVal.EndValue = 200
pField.CurrentValues.Add(rVal)
paramFields.Add(paramField)

Set the parameter fields collection into the viewer control:

VB
crystalReportViewer1.ParameterFieldInfo = pFields

crystalReportViewer1.ReportSource = "c:\codeproject\About.rpt"

How to use this control

Step #1

Re-build the control.

Step #2

  1. Open a new project, add a crystalreportviewer control and a Button.
  2. Add this control by going to the toolbox control. Select the ‘General’ Tab, then right click on the mouse, then you will find an option in the popup as ‘Customize toolbox’. This will bring a window. Then select the tab called ‘.NET framework components’. Using the Browse button, add the UserControl. Then it will show it on the toolbox.
  3. Then add the control in our current form.
  4. Then write the following code for the Button as follows:

    Note: If necessary, change the path of the report filename.

    VB
    CryRepControl1.ReportFileName = "d:\VB.Net\AnyTest\MyProfile.rpt"
    CryRepControl1.ParameterFields(0) = "MyName;" & "J. Franklin Kennedy"
    CryRepControl1.ParameterFields(1) = "MyAge;" & "31"
    CryRepControl1.ParameterFields(2) = "DOB;" & "09/01/1973"
    CryRepControl1.ParameterFields(3) = "Married;" & "True"
    RptViewer1.ParameterFieldInfo = CryRepControl1.ParamFields
    RptViewer1.ReportSource = CryRepControl1.CrySet()
  5. Run the project.

    Note: The report ‘MyProfile.rpt’ is also in the ZIP file.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

kennedy_franklin
Web Developer
Kuwait Kuwait
I am J. Franklin Kennedy, Working as a programmer in application software developement.


Comments and Discussions

 
Questionhow to add the code to enable integrated security in the report in Visual Studio 2012 Pin
Andrzej Rapp27-Jan-21 2:33
MemberAndrzej Rapp27-Jan-21 2:33 
GeneralDisable parameter prompting and passing boolean parameter to Crystal Report 9 in VB.Net 2008 Pin
crystal_john6-Jun-11 1:22
Membercrystal_john6-Jun-11 1:22 
GeneralMy vote of 5 Pin
inamgul26-Apr-11 20:56
Memberinamgul26-Apr-11 20:56 
GeneralMy vote of 5 Pin
salmanansari14-Jan-11 23:31
Membersalmanansari14-Jan-11 23:31 
Questionhow to use Pin
Rabbiiit29-Sep-10 20:37
MemberRabbiiit29-Sep-10 20:37 
Generalthank you Pin
oss_au5-May-10 21:24
Membeross_au5-May-10 21:24 
GeneralDiscrette param question Pin
Raul H Chico13-Jan-10 21:41
MemberRaul H Chico13-Jan-10 21:41 
General:) :-D thank you very much. Pin
Аslam Iqbal19-Sep-09 9:23
professionalАslam Iqbal19-Sep-09 9:23 
GeneralExample project in vb with your class. Pin
FastBurst17-Aug-09 12:49
MemberFastBurst17-Aug-09 12:49 
QuestionCrystal Report Pin
arbind21037917-Oct-07 0:51
Memberarbind21037917-Oct-07 0:51 
AnswerRe: Crystal Report Pin
Аslam Iqbal19-Sep-09 9:35
professionalАslam Iqbal19-Sep-09 9:35 
AnswerRe: Crystal Report Pin
Sreyas M N22-Oct-09 23:02
MemberSreyas M N22-Oct-09 23:02 
GeneralMultiple Parameter values for 1 parameter Pin
ckwizard7730-Jul-07 1:46
Memberckwizard7730-Jul-07 1:46 
Questionhow can i do it in .NET? Pin
The Putxi Project26-Mar-07 22:11
MemberThe Putxi Project26-Mar-07 22:11 
GeneralSubreport Parameter count Posing Problem Pin
RashmiSaggi19-Mar-07 15:28
MemberRashmiSaggi19-Mar-07 15:28 
Questionhelp me with this error "Buffer too small for string or missing null byte" Pin
osbeng4-Jan-07 4:50
Memberosbeng4-Jan-07 4:50 
Generalthe control works in web crystal Pin
vladimirbc16-Aug-06 15:22
Membervladimirbc16-Aug-06 15:22 
GeneralError while trying to add control to the form Pin
HarSha9916-May-06 2:39
MemberHarSha9916-May-06 2:39 
GeneralAddind data to the report in a loop Pin
worm3018-Feb-06 19:41
Memberworm3018-Feb-06 19:41 
Generalcascading prompts parameter values from vb.net Pin
surruchie25-Jan-06 7:21
Membersurruchie25-Jan-06 7:21 
GeneralRe: cascading prompts parameter values from vb.net Pin
Dan_Gearhead19-Jan-09 20:54
MemberDan_Gearhead19-Jan-09 20:54 
QuestionAbout Crystal Pin
| Muhammad Waqas Butt |23-Sep-05 4:22
professional| Muhammad Waqas Butt |23-Sep-05 4:22 
GeneralWB Crystal Report Problem~ Pin
| Muhammad Waqas Butt |22-Sep-05 23:30
professional| Muhammad Waqas Butt |22-Sep-05 23:30 
GeneralCrystal Report Problem Pin
| Muhammad Waqas Butt |22-Sep-05 23:29
professional| Muhammad Waqas Butt |22-Sep-05 23:29 
Generaltechnical question Pin
Ameen AboDabash28-May-05 20:14
MemberAmeen AboDabash28-May-05 20:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.