views:

6049

answers:

7

In Reporting Services I would like to add a parameter that contains data from a custom code block. Ideally, I would be able to run the following code (this is a simple testing example):

Function GetPeriods() As String()
 Dim values As System.Collections.ArrayList = 
    New System.Collections.ArrayList()
 For i as integer = 1 to 24
    values.Add(i)
 Next
 Return values.ToArray()
End Function

and put the following in the "Text Field" of the parameter:

=Code.GetPeriods()

However, when I run the report, the parameter I apply this to is disabled and empty. Is there a different technique that should be used? Or am I doing something wrong?

A: 

Everything I've seen requires parameters and their respective settings to be part of the RDL.

That being said, if you're going to "hardcode" the values, you could create a dataset just for the report, perhaps in XML, or if it needs to be programmatically driven, do it in a web service.

Gene
+1  A: 

If you're using SQL 2008 Reporting Services then you can have a look at this page which introduces the concept of using custom assemblies.

If you're using SQL 2005 Reporting Services then this link is the one you want.

It's a mostly trivial thing, simply compile your code into a class library and follow the instructions provided to allow your report to reference it.

Timothy Walters
+1  A: 

You can create the same stored procedure on SQL Server and load parameter values from that procedure.

+1  A: 

You are returning an array item (an array of strings) into a text field. Instead, try returning a plain string. That should work. If you would still like to return an array list, you must basically bind it to a list control in your RDL. You can definitely do that with dataset extensions. However, I am not sure if there is any other easy way. Check the proprties of the list control and see if it allows you to directly bind to an array list.

msvcyc
A: 

i ckecked ur code...only thing wrong is that ur function returns String(). When I changed ur method signature to return Array. It worked fine, in my report Change signature to " Function GetPeriods() As Array "

A: 

To access your members/functions implemented in custom code of SSRS report you should set the access modifier to "Public":

Public Function GetPeriods() As String
...

see article Writing Custom Code in SQL Server Reporting Services

Max Gontar
+1  A: 

I've been trying to do this same thing, set a simple list of parameter values from report code. None of the links in any of these answers shows how to do this and after quite a bit of digging around I don't think it's even possible. Yes it is possible to get the values from a database query, from a web service, or from a custom assembly, but each of these creates a lot of overhead compared to getting the list from a simple function call like =Code.GetValues(), where the function uses a For loop to create the values.

msvcyc is correct in pointing out that the parameter is expecting a string value, but the function is returning an array. I changed the return type to Array as suggested by prashant sable, but the select list is still grayed out, it does not work. And coldice is correct in saying that the access modifier should be Public.

In my digging around I found an article by James Kovac from 2005 that pointed out why this is not possible. The Parameters class has a get method, but no set method. In the VS 2008 object browser for SSRS 2008 the object name has changed, but it still does not contain a set method (see Microsoft.ReportingServices.Interfaces.IParameter.Name or .Value).

My current workaround is to just hard code the list of values, but if your value list needs to be dynamic then your only choices are database queries, web services, or custom assemblies. I think the easiest workaround of these three is to get the values from the database engine, as suggested by oleksiy.t, as long as you can write a query to return the value list you want. Your list of integers, or my list of time intervals, would both be easy queries to write. Otherwise you will need to use one of the other two workarounds.

Bratch