This is a migrated thread and some comments may be shown as answers.

Building report parameters programmatically

3 Answers 565 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
John Staggs
Top achievements
Rank 1
John Staggs asked on 30 Oct 2009, 03:54 PM

For my application I need to build report parameters programmatically.  I have tried to follow the examples available but I cannot get the parameters to display as a dropdown list of values.  The parameter does show up in the viewer but as a textbox.  Below is the code that I am using to build my parameter -  

 

 

Dim vals As New ArrayList   
 <fill vals with strings of option values> 
 
Dim param As New ReportParameter   
param.Type = ReportParameterType.String  
param.UI.Visible = True   
param.UI.MultiValue = True   
param.Value = vals   
   
Me.ReportParameters.Add(param)  
 
 

This is wrapped in a routine that is called when the report is bound to it's data source.  When I try to call the routine from the report's constructor it results in a run-time error.

My questions are:
1. How do I get my options to show up as a dropdown list?
2. When should this routine be called? 
3. Is it possible to build it with a value/key pair rather than just a list of values? 

----------------------------------------------------------------------------------------------------------------
Upon further investigation, either programmatic definition of parameters is not supported or the documentation is severely lacking because I have tried every example (there are just a few) that I can find and it either results in no parameter or a run-time error.  For example, just trying to add a simple test parameter with the following code results in no parameter in the viewer:

    Public Sub New()  
        InitializeComponent()  
        ReportParameters.Add("Test", ReportParameterType.String"test")  
    End Sub 

Thanks,
Dave

 

 

 

 

 

 

 

3 Answers, 1 is accepted

Sort by
0
Chris Gillies
Top achievements
Rank 1
answered on 02 Nov 2009, 03:51 PM
First you need to clarify whether you really need to add the parameters programmatically. From my expertise, such approach is not needed as the report parameters are used to filter the loaded datasource i.e. there is no unexpected data that would require you to add params dynamically.
The first code snippet uses definition report parameters and therefore you cannot use that "routine" in report events but only in report constructor. This fact on the other hand makes this code irrelevant as creating the params through the designer leads to the same thing and saves you time.
The second code snippet works fine on my end, if I add a textbox with value =Parameters.Test in my report.

Cheers
0
Gersh
Top achievements
Rank 2
answered on 10 Nov 2009, 07:18 PM
Hey John,
   I think you may want to populate the list of parameters
(ReeportParameter rp).UI.AvailableValues.DataSource = data as ArrayList; 
my data can be a sqlsource or any type of collection.
If you figure out how to fill initial value.
0
Kristy
Top achievements
Rank 1
answered on 17 Nov 2009, 05:03 PM
Food for Thought:
There are all sorts of reasons why someone would want to add a parameter programatically. There may be conditions to check before determining which parameters (and available values) to display based on user permissions or conditions of data at the moment the report is being run. Maybe they would want to reuse parameters on several reports (in this case, they would have to set the parameter classes outside the report then add them to the report parameter collection in code). In John's example, you are right, maybe he doesn't need to programatically set it, but maybe there's info he's not sharing, or maybe he just prefers code. Some developers feel they have more control (and a better understanding) by typing code rather than using a UI designer.
Tags
General Discussions
Asked by
John Staggs
Top achievements
Rank 1
Answers by
Chris Gillies
Top achievements
Rank 1
Gersh
Top achievements
Rank 2
Kristy
Top achievements
Rank 1
Share this question
or