Another bit for dynamic ReportViewer control-handling: Parameters in more depth

by Lisa Nicholls Fri, September 21 2007 01:49

FWIW I dearly love URL Access in SQL Server Reporting Services.  Well, to be exact, I love REST interfaces in general. (I don't know why MS couldn't use the perfectly good name already available for this feature.) 

I didn't discuss this in much detail in my last post on dynamic use of ReportViewer controls, although it contains a quick parameterization of the control as part of the general dynamic process that the RDLDocumenter TestHarness goes through. But it seems that this is something people need, so here goes...

This time, just for fun, we'll be using server mode reports (RDLs) instead of local mode (RDLCs), and the webform ReportViewer control instead of the winform version of XMLRSDocs' TestHarness form uses.  Same process, though.

Barry Williams would like his users to use a REST-style API to call up reports in a webform. An optimal solution will (a) take little to no programming -- the reports are server-based, not local-mode -- and (b) not take any adjustment when the parameters available for his report(s) change.  Let's put something together to handle that.

Barry is a data guy, so I will expect him to drive this solution from a database with more smarts than I'm showing in this example.  Here, I mostly want to show how you pull lthe parameters out of the URL request and apply them to the report and, rather than show a full data-driven solution, I'll use the following simple strategy to make the solution generic:

  • iterate through all the parameters sent in the URL
  • determine which ones are "mine" by looking for a naming convention -- here, I'll use a prefix of r: for "my" example parameters
  • apply those values to my report

As an example, in the following URL, "test" will be ignored and the other two parameters will be applied:


http://myServer/myApp/ReportViewerParametersDemo.aspx?
   r:Status=Cancelled&r:ExcludeInternal=false&test=Something


Ready? 

If you want to play along at home, or use this code, all you need to do in Visual Studio to reproduce my ASPX page is this:

  • Create a new ASPX page ("New web form")

  • In the Code view, blow away the code-behind information (CodeFile="Default2.aspx.vb" Inherits="Default2") in the page directive, so we're left with

    <%@ Page Language="VB" AutoEventWireup="false" %>


    ... I honestly see no reason to use a code-behind page rather than a real class built into a DLL if you have serious code to put into an ASPX page.  IAC, we don't have serious code here.

  • In the Design view, drag and drop a ReportViewer control and a Label control (for some debugging and eventual validation feedback) onto the page surface.

  • "Point" the ReportViewer control at your server report, using the ReportViewer.ServerReport ReportPath and ReportServerURL properties.

Now go back to the Code view and add the server-side block you see below into the page, to handle the page load event.

There are only a couple of tricky things about this, so I'll just note them quickly for you here:

  • For those of you who are not used to doing Asp.NET work, we're using Request.QueryString.  For more extensive requirements beyond simple URLAccess GET syntax, you would use Request.Params.
  • You may be surprised, or confused, by the use of the Split function on the value of being passed through the querystring.  A list of values that all have the same parameter name -- for example, the choices in a multi-select HTML listbox -- are passed by browser-clients as a comma-delimited string to the server.  Essentially, multi-value report parameters model this behavior; they hold collections of values as an array.  If there is no comma, there's only one value, but if there are more, you need to split them out into the array that the report "expects" to see for this parameter. 

Here is a parameterized URL example showing three values for the "Status" multi-valued parameter:


http://myServer/myApp/ReportViewerParametersDemo.aspx?
   r:Status=Cancelled,Pending,Tentative&r:ExcludeInternal=false
 

.. the Split function will take care of passing Cancelled, Pending, and Tentative as the separate values to this parameter.

Enjoy!


<script runat="server">

Protected Sub Page_Load( _
  
ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

   Dim sb As New System.Text.StringBuilder()
   Dim p(0) As Microsoft.Reporting.WebForms.ReportParameter
   Dim i As Integer = 0
   ' If we were truly datadriving this we would validate values by type
   ' as well as the actual param names
   For Each s As String In Request.QueryString.Keys

      ' feedback to put in Label:
      sb.Append(s & "=" & Request.QueryString.Item(s) & " "

      If Left(s, 2) = "r:" Then
         ' this querystring name value pair fits our naming convention.
         i += 1
         Array.Resize(p, i)
         p.SetValue( New
          Microsoft.Reporting.WebForms.ReportParameter(s.Substring(2), _
              Request.QueryString.Item(s).Split(",")), i - 1)
      Else
         ' Don't send -- the querystring contains
         ' a name value pair that does not belong to us.
      End If
   Next

   If
sb.Length = 0 Then
       Me.Label1.Text = "You didn't send a query string."
   Else
      Me.Label1.Text = "You sent: " & sb.ToString()
      If i > 0 Then
         ' this should have a try-catch around it
          Me.ReportViewer1.ServerReport.SetParameters(p)
         ' Your catch could put some feedback into the page,
         ' just as I've put the user's choices into
         ' the label which is the only other control on this page...
      End If
   End If
   sb = Nothing

End
Sub

</script>

Tags:

ASP.NET | Reporting

Comments (36) -

11/11/2007 6:28:03 AM #

Jayce

Thanks for the tips. ;)

Jayce Malaysia

2/12/2008 2:16:44 PM #

vijaya

I am gettign an error with

Microsoft.Reporting.WebForms.ReportParameter

as type to be defined what do I need to do

vijaya United States

2/13/2008 12:04:45 AM #

L

Hi Vijaya,

Are you really getting an error or is this just a failure of intellisense?

If the latter, it happens to people all the time, but it should be okay at runtime.  

If the former, when you dragged and dropped the control into your form, a Microsoft.Reporting.WebForms reference should have been added to your project.  If this did not happen, you need to put one there...

L United States

5/9/2008 8:41:23 AM #

b471code3

Great procedure to walk through.  Just wondering why I cannot get my parameter to be accepted?  I have a server report which has a hidden parameter named "cCo" and it is displayed in my label but not set to my report.  I just copied and pasted your code and for some reason it doesn't sent the parameter to the report.  Any thoughts?

b471code3 United States

5/9/2008 9:53:05 AM #

b471code3

Little did I know I had to put a "r:" in front of my parameter in order for it to work!
Thanks

b471code3 United States

5/9/2008 6:46:48 PM #

L

>> Little did I know

You don't really have to put an "r:" in front of the parameter!

As I said in the post:

>>determine which ones are "mine" by looking for a naming convention -- here, I'll use a prefix of r: for "my" example parameters


... in my sample code, I distinguished "my" parameters from other parts of the query string in which I was not interested.  Remember that the URL might have other parts to its query string that you don't want to pass on to the Reporting Server.  This was just a convention that I adopted for the example, and then (of course) carried through in my generic code that parsed the parameters to figure out what to send on:

>>
If Left(s, 2) = "r:" Then
' this querystring name value pair fits our naming convention.
' [... we'll process this parameter here... ]
Else
  ' Don't send -- the querystring contains
  ' a name value pair that does not belong to us.
End If
<<

But there is nothing "sacred" about it.  (It *is* a sacred responsibility to read the comments in sample code, they are trying to tell you something <g>.)

Good luck.

L United States

10/13/2008 3:50:04 PM #

tutyu

sfgdsfdsfdsfsffsfsfsfsfsdffsdfsdfs
ffsdf
fsdfds
dssd
fssfsfds
fssfsssfsfsfss

tutyu India

1/24/2009 2:10:37 PM #

gareth

Hi Lisa

I've used your example, (converted into c#) to pass multi-valued parameters to my report as below.  It works, except only the first parameter value is being used in the report.  Any tips?

Thanks
Gareth
//-----------------
Array.Resize(ref repParamArr, i);
string[] vals = Request.QueryString[s].Split(',');
repParamArr.SetValue(new ReportParameter(s.Substring(6), vals, false), i - 1);
this.ReportViewer1.ServerReport.SetParameters(repParamArr);

gareth South Africa

1/24/2009 2:12:40 PM #

gareth

Hi

Sorry.  My mistake.  I had used the same value for the 2nd parameter.

Thanks

Gareth

gareth South Africa

1/24/2009 11:29:17 PM #

&gt;L&lt;

It's always something <g>.  Thanks for reporting back...

>L< United States

6/14/2009 6:25:21 PM #

newpro

it is working perfectly for me.thanks lisa

newpro United States

6/23/2009 8:38:07 AM #

Internet marketing

Nice post.I am writing some Silverlight modules for an ASP.NET MVC framework and this fits perfectly..

Internet marketing United States

7/2/2009 5:38:01 AM #

Emo Clothes

Great procedure to walk through. Just wondering why I cannot get my parameter to be accepted? I have a server report which has a hidden parameter named "cCo" and it is displayed in my label but not set to my report. I just copied and pasted your code and for some reason it doesn't sent the parameter to the report.  Emo Clothes

Emo Clothes United States

7/2/2009 7:47:11 AM #

&gt;L&lt;

Do you have other parameters that are being accepted, and this is the only one that is not?  Or is this your only parameter?  

In the former case, what's the value you're trying to send?  

In the later case, please notice that I used a prefix to distinguish what was being sent that I want to pass on to the report, and other parameters that might be in the query string but I don't want to send on.  In my example, the prefix is r:.  So you have to either use that prefix or remove the part of the sample code that is *looking* for that prefix.

>>
If Left(s, 2) = "r:" Then
' this querystring name value pair fits our naming convention.
' [... we'll process this parameter here... ]
Else
' Don't send -- the querystring contains
' a name value pair that does not belong to us.
End If
<<

>L<

8/9/2009 5:56:34 AM #

radiateur inertie

For me everything is very clear, it's very good work. Thanks

radiateur inertie

8/28/2009 11:36:06 AM #

voyance mail

Everything is very clear.Thank you very much.

voyance mail

1/23/2010 8:07:58 AM #

quiche recipe

Thanks for the code!

quiche recipe United States

1/29/2010 2:45:18 PM #

p&#228;rmar

Nice to be reading your blog again. Thanks for posting this informative blog post. Best regards, Zachary Kennedy @ parmar

pärmar

2/17/2010 11:54:48 PM #

Nisha Cieslak

Superb article, We just handed this to a colleague who was accomplishing a minor study on that. And he actually bought me lunch because I found it for him smile So let me rephrase that: Thanks for lunch!

Nisha Cieslak United States

2/18/2010 12:04:40 AM #

Liana Petross

Admiring the determination you place into your web log and comprehensive details you offer! I will bookmark your blog and have my buddies also look at up here often. Thumbs up!

Liana Petross United States

2/24/2010 7:00:09 AM #

jasmin live

Have you ever considered adding more videos to your blog posts to keep the readers more entertained? I mean I just read through the entire article of yours and it was quite good but since I'm more of a visual learner,I found that to be more helpful.

jasmin live United States

4/28/2010 4:22:07 AM #

Jump Higher

In the later case, please notice that I used a prefix to distinguish what was being sent that I want to pass on to the report, and other parameters that might be in the query string but I don't want to send on.  In my example, the prefix is r:.  So you have to either use that prefix or remove the part of the sample code that is *looking* for that prefix.

Jump Higher United States

5/1/2010 12:19:41 AM #

Speaking English

Great Great great. Last post on Reportviewer was mind blowing and this one added a lot of information ... Keep sharing stuff like this
Kevin Leo

Speaking English United States

5/8/2010 10:08:43 PM #

pk16

Thanks for sharing the code with us. I am actually trying to play a little bit something with your code, and will tell you what happens next. Smile

pk16 United States

5/10/2010 11:12:06 AM #

information

Thank you so much. I am actually playing along with the codes right now, and I got so amazed with what I can do with it. Hope more people like you will continue to share informative posts. Keep it up!

information United States

5/10/2010 11:12:21 AM #

information

I've always wanted to use server mode reports before but I didn't know how to. Thanks for this useful post. I appreciate your effort on this tutorial. Looking forward to reading more posts from you.

information United States

5/11/2010 11:36:11 AM #

ilmci

At least now I have an idea on how a server mode report looks like. I've actually heard one before, but never got the chance to take a look at the code itself. Thanks for posting this great information.

ilmci United States

5/18/2010 7:17:13 PM #

istanbul private tours

The article was well explained. We have experience this kind of problem in our line of work. Thank you for the very informative post.

istanbul private tours United States

5/26/2010 10:37:40 PM #

starcraft 2 replays

This article is very well written and I know that the author have been very proud of his work because he knows that he will help and have helped other people in solving their problem. Thank you very much and I encourage you to write more.

starcraft 2 replays United States

6/22/2010 7:01:31 PM #

Circuit DIY

This tutorial is very helpful to all who need it, because you've explained perfectly and easy to understand. very pleased to be able to visit your site

Circuit DIY United States

8/12/2010 9:31:16 AM #

Internet Marketing Consultant

It's taken me about 40 minutes to find this exact tutorial. Thanks a ton. It has literally saved me hours of time. I'll continue reading your posts in the future.

Internet Marketing Consultant United States

9/4/2010 11:05:45 PM #

Consumer Debt Management UK

Thanks for the code! Excellent information (as always).

Consumer Debt Management UK United Kingdom

9/15/2010 12:27:59 AM #

starcraft 2 strategy

Hows it Going great and fun to read.  I am a big follower of the topics talked about.  I also enjoy reading the other peoples thoughts, but it seems like that a great deal of followers should keep on topic to try and add something to the original topic.  I would also encourage all of you to bookmark this blog to your favourite service to help get the word out.  Thanks

starcraft 2 strategy United States

9/19/2010 3:01:03 PM #

Wood privacy fence

ayce Thanks for the information and great ideas. Keep on believing in your dreams and you will achieve success..good luck.

Wood privacy fence United States

5/7/2011 4:14:33 AM #

Vizz Media

Bless you for your blog post, that’s some fairly useful information.

Vizz Media India

11/1/2011 2:01:18 PM #

Wayne

I tried and it worked fine - thanks Lisa!

Wayne United States

Add comment




  Country flag
biuquote
  • Comment
  • Preview
Loading