Click here to Skip to main content
6,634,665 members and growing! (15,710 online)
Email Password   helpLost your password?
Languages » VB.NET » Samples     Intermediate License: The Code Project Open License (CPOL)

How to pass discrete parameters to Crystal Reports

By kennedy_franklin

This is a simple and small user control that will help programmers to pass discrete parameters to Crystal Reports without writing lengthy code.
VB, Windows, .NET 1.0, Dev
Version:2 (See All)
Posted:10 Jan 2005
Views:183,400
Bookmarked:56 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
15 votes for this article.
Popularity: 3.92 Rating: 3.33 out of 5
1 vote, 6.7%
1
4 votes, 26.7%
2
2 votes, 13.3%
3
2 votes, 13.3%
4
6 votes, 40.0%
5

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

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

' 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:

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

Set the parameter fields collection into the viewer control:

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.

    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)

About the Author

kennedy_franklin


Member
I am J. Franklin Kennedy, Working as a programmer in application software developement.


Occupation: Web Developer
Location: Kuwait Kuwait

Other popular VB.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 23 of 23 (Total in Forum: 23) (Refresh)FirstPrevNext
General:) :-D thank you very much. Pinmemberkisukortechai10:23 19 Sep '09  
GeneralExample project in vb with your class. PinmemberFastBurst13:49 17 Aug '09  
QuestionCrystal Report Pinmemberarbind2103791:51 17 Oct '07  
AnswerRe: Crystal Report Pinmemberkisukortechai10:35 19 Sep '09  
AnswerRe: Crystal Report PinmemberSreyas M N0:02 23 Oct '09  
GeneralMultiple Parameter values for 1 parameter Pinmemberckwizard772:46 30 Jul '07  
Questionhow can i do it in .NET? PinmemberThe Putxi Project23:11 26 Mar '07  
GeneralSubreport Parameter count Posing Problem PinmemberRashmiSaggi16:28 19 Mar '07  
Questionhelp me with this error "Buffer too small for string or missing null byte" Pinmember5:50 4 Jan '07  
Generalthe control works in web crystal Pinmembervladimirbc16:22 16 Aug '06  
GeneralError while trying to add control to the form PinmemberHarSha993:39 16 May '06  
GeneralAddind data to the report in a loop Pinmemberworm3020:41 18 Feb '06  
Generalcascading prompts parameter values from vb.net Pinmembersurruchie8:21 25 Jan '06  
GeneralRe: cascading prompts parameter values from vb.net PinmemberDan_Gearhead21:54 19 Jan '09  
QuestionAbout Crystal PinmemberMuhammad Waqas Butt5:22 23 Sep '05  
GeneralWB Crystal Report Problem~ PinmemberMuhammad Waqas Butt0:30 23 Sep '05  
GeneralCrystal Report Problem PinmemberMuhammad Waqas Butt0:29 23 Sep '05  
Generaltechnical question PinmemberAmeen Abudbush21:14 28 May '05  
GeneralHow to convert RDC from crystal 8.x to 9.0 PinmemberBenj1016:21 18 Apr '05  
GeneralHow to add username and password PinmemberBenj106:30 14 Apr '05  
AnswerRe: How to add username and password PinmemberCoding12345615:26 16 Nov '06  
GeneralPlease how to this in C# Pinmemberdazbuda7:35 16 Feb '05  
AnswerRe: Please how to this in C# Pinmember| Muhammad Waqas Butt |6:50 15 Oct '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 10 Jan 2005
Editor: Smitha Vijayan
Copyright 2005 by kennedy_franklin
Everything else Copyright © CodeProject, 1999-2009
Web22 | Advertise on the Code Project