Home   |  Articles   |  Resources   |  Humor   |  Feedback       

  Login   Register 

Ads Via DevMavens


Using GetReportParameters in Reporting Services

Posted by on Saturday, May 08, 2004

This example demonstrates how to use GetReportParameters to retrieve valid values for a report parameter.

If you want to build your own UI front end to SQL Server Reporting Services, one web method you’ll need to become familiar with is GetReportParameters. GetReportParameters will return metadata about the parameters in a report, including names, types, and valid values.

This article assumes you are familiar with adding parameters to a report in SQL Server reporting services, if not, you can visit the resource links at the bottom of the article. This article also assumes you have established a web reference to the reporting service, for more details on this step see our previous article.

When you create a report parameter in Reporting Services, you can assign the parameter a name, a data type, and specify if null is a legal value for the parameter. A report designer can also assign valid values for a report parameter, which can be done using a query. For example, a report from the example Northwind database in SQL Server might have a parameter for product category. To ensure the user selects a product category which exists in the database, a designer can create a new dataset for the report and use the following query:

SELECT CategoryID, CategoryName FROM Categories.

If we were building our own front end tool for report viewing, we would want to show the valid values for a parameter to the user. Using the GetReportParameters as shown below will give us the information we need.

ReportingService rService = new ReportingService();
rService.Credentials = 
	System.Net.CredentialCache.DefaultCredentials;

string historyID = null;
bool forRendering = true;
ParameterValue[] values = null;			
DataSourceCredentials[] credentials = null;
ReportParameter[] parameters;

parameters = rService.GetReportParameters
	(
		"/Northwind Reports/SampleReport",	
		historyID,
		forRendering,
		values,
		credentials								
	);

foreach(ValidValue v in parameters[0].ValidValues)
{
	Response.Write(v.Label + ": " + v.Value);
	Response.Write("<br>");
}

The important point in the above code is to pass a value of ‘true’ in the forRendering method parameter. If the valid parameter values are based on a query, setting this parameter to true will tell Reporting Services to execute the query and return the results in the ValidValues array of ValidValue objects. The code above should print the following to the web form:


Beverages: 1
Condiments: 2
Confections: 3
Dairy Products: 4
Grains/Cereals: 5
Meat/Poultry: 6
Produce: 7
Seafood: 8

It is also possible to set cascading parameters. For instance, we might setup a product parameter for our report which has a dependency on the category selected:

SELECT ProductID, ProductName FROM Products WHERE CategoryID = @CateogryID

This restricts the products a user can select to the products in the selected category. What happens when we call GetReportParameters to look at the valid values?

The code presented earlier will still work for the first parameter (CategoryID). We can still see valid values for parameter[0]. The second parameter (ProductID), parameter[1], however, will have a null reference for the ValidValues property. The State property for parameter[1] will be set to ParameterStateEnum.HasOutstandingDependencies, and the Dependencies property (a string array) will have a length of 1, with Dependencies[0] set to “CategoryID”.

To resolve the dependency and find valid values for our ProductID parameter, we will need to select a CategoryID and call GetReportParameters again. Let’s append the following code to our earlier sample.

values = new ParameterValue[1];
values[0] = new ParameterValue();
values[0].Name = parameters[0].Name;
values[0].Value = "8";

parameters = rService.GetReportParameters
	(
	"/Northwind Reports/Report2",	
	historyID,
	forRendering,
	values,
	credentials								
	);

foreach(ValidValue v in parameters[1].ValidValues)
{
	Response.Write(v.Label + ": " + v.Value);
	Response.Write("<br>");
}

In the code above we create an array to hold a ParameterValue object for CategoryID. We set the CategoryID to 8, which is the seafood category. Now the list of valid values for ProductID will appear in ValidValues.


Ikura: 10
Konbu: 13
Carnarvon Tigers: 18
Nord-Ost Matjeshering: 30
Inlagd Sill: 36
Gravad lax: 37
Boston Crab Meat: 40
Jack's New England Clam Chowder: 41
Rogede sild: 45
Spegesild: 46
Escargots de Bourgogne: 58
Röd Kaviar: 73

If you are creating your own reporting UI, you can check the Dependencies and State properties to determine when multiple invocations of GetReportParameters will be required. One additional tip: when a parameter has a dependency, it will always appear at a higher index in the ReportParameter array.

by K. Scott Allen

Resources

MSDN: Using Parameters in a Report

Read more Reporting Services articles on OdeToCode.


Comments:

passing Multi value parameter to a report
By krvaishnavi on 5/25/2004
Can you give me some help on how to pass multivalue parameter to a report.
Say, my report will give the details of an employee or employees (select * from employee where employeeId in(@pId))
now @pID values can be multiple also.
How should i pass these values from aspx file.

re: Multi Value parameter
By scott on 6/11/2004
Hi K,

Passing multiple values for a parameter is not supported in this first release of Reporting Services, but I know Microsoft is looking at adding this capability in future version. There is a workaround available here: http://www.gotdotnet.com/Community/UserSamples/Details.aspx?SampleGuid=2E882C0A-8D2B-4EAD-81BE-8E66C0941A18

SQL Server Reporting Services
By mithunv on 8/24/2004
I need to implement sorting in my report. 1) I should have some up-down arrows beside the header fields. 2) On clicking them, the data should get sorted. How can we implement this sorting feature in Reporting Services?

How to render the report in a User Control
By jensenkd on 8/27/2004
Scott,
Great article and thanks for your time and knowledge.

I am trying to create a User Control that renders dashboard graphs from reporting services.

I created the .rdl file, and the user control. I am successful at sending the reports parameters via the Web Service and then calling the rs.Render() Method.

However, when I use the HTML4.0 Format, I just get an empty page.

My questions are: Is this the proper way to create a User Control for Reports? Any ideas on what I could be doing incorrectly?

Thanks,
Kevin

Multiple Values
By eralper on 9/5/2004
Hi,

You can send multiple values in the parameters as '1,2,3,4'.
By writing a user defined function whish is returning a table, you can split the parameter into id values and make changes on your code and join this table.

I saw this example in AdventureWorksDW database

I hope this helps...

Multiple Parameters
By eralper on 9/6/2004
Hi,
You can use the function coded in the article http://odetocode.com/Articles/58.aspx for splitting a multiple parameter string into its value list

Report Parameters
By jwatsonzia on 9/13/2004
I am having trouble getting it right. I want to pull a list of the report parameters for a report so that I know which values to render with. I have multiple reports and so I need to check first. Can I use the GetReportParameters to do this? I get a problem with authorization when I use the code from the article. I must be doing something wrong. Can you help? Thank you!
Jenny

parameter
By suresh_gprec on 9/29/2004
ok

Multiple parameters for Reports
By thomaslnickelsen@hotmail.com on 11/14/2004
Hi Scott,
Great article. I assume that you are talking about multi-value parameters (as you might use in OLAP) and not multiple parameters here. I am pretty sure that Reporting Services supports arrays of parameters though I can't really test it until tomorrow. However, if multiple parameters are not supported I suppose you could pass a combined parameter text string to your stored procedure and parse it there with a function. (Yuk.) I will let you know what I find out.

how to show the parameters value
By reza5531 on 11/15/2004
Can you tell me a way I can show all the parameters of an specific report and their value(s) which are based on the Query on the datagrid.

About ReportParameter.DefaultValuesQueryBased property
By emil_prager on 3/18/2005
Hello there,

I liked your post about some ReportParameter properties, which is a domain where I am directly interested, so I want to ask you a question.

I have to develop a web application that is a customized and extended front-end for Reporting Services (v. 8.0, 9.0), replacing the standard interface Report Manager.
This web app uses a previously created middle layer (so called here "User Interface Abstraction Layer") between the Web service proxy class and the GUI choosen (which generraly can be a Windows GUI, or, as in the my case, a Web GUI).
A crucial point in this application (acctually, in the UI Abstraction Layer) is the posibility that different users have different DefaultValue-s for the same ReportParameter (for the same report), and that this DefaultValue should not be a fixed one, but an autoupdating one: for example, one user wants that some sales report should be runned with the sales from the past 2 weeks, while another user wants the same same report generated over the past month; thus, the DefaultValue is different for the two users mentioned, and the value itself is autoupdating (not a fixed value, but always something like
dateAdd( ... )
etc.).

So, the DefaultValue should be given, in this example, by a scalar function which we can imagine to be like this:
dbo.fn_SalesBeginDate( @userID, @reportID, @reportParamID, @nSubscriptionID, @dtCurrent_Timestamp )
which has the parameters:
@userID, @reportID, @reportParamID
(whose values are known at runtime)
@nSubscriptionID
(this occurrs eventually, the in case that the same user wants the same report generated with differnt inputs on different subscriptions), and
@dtCurrent_Timestamp
(the current dateTime passed as a parameter (because getDate() (Current_Timestamp) is non-deterministic and so is not permitted in the body of the function)).


Now, the question itself (we restrict the discussion on the simpler case of CreateSubscription() method, leaving the general case, CreateDataDrivenSubscription() method, for a later time):

How can I set the ParameterValue corresponding to a ReportParameter that have the DefaultValuesQueryBased property set to true?
More speciffically, how can I set the ParameterValue.Value to the returned value of the function
dbo.fn_SalesBeginDate( @userID, @reportID, @reportParamID, @nSubscriptionID, @dtCurrent_Timestamp )
, knowing that I give the values for all the scalar function parameters at run time ?


Thanking very much for your intention,

Emil Prager

Copyright 2004 OdeToCode.com 


The Blogs
Subscribe to the OdeToCode blogs for the latest news, downloads, new articles, and quirky commentary.
New Articles
C# 3.0 and LINQ
C# 3.0 introduced a number of new features for LINQ. In this article we'll examine the new features like extension methods, lambda expressions, anonymous types, and more.

Introduction To LINQ
This article is an introduction to LINQ and provides examples of using LINQ to query objects, XML, and relational data.

What ASP.NET Developers Should Know About JavaScript
This article looks at JavaScript from the perspective of a C# or Visual Basic programmer. See how to apply object oriented techniques to your JavaScript code.

Most Popular Articles
Master Pages In ASP.NET 2.0
Master pages in ASP.NET are the key to building a professional web application with a consistent, easy to maintain layout.

ASP.Net 2.0 - Master Pages: Tips, Tricks, and Traps
MasterPages are a great addition to the ASP.NET 2.0 feature set, but are not without their quirks. This article will highlight the common problems developers face with master pages, and provide tips and tricks to use master pages to their fullest potential.

Table Variables In T-SQL
Table variables allow you to store a resultset in SQL Server without the overhead of declaring and cleaning up a temporary table. In this article, we will highlight the features and advantages of the table variable data type.

Contribute Code
Privacy
Consultancy