CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Peter's Gekko

public Blog MyNotepad : Imho { }

Using SQL reporting services in an asp.net application with some notes on report parameters

In the Crystal days adding reports to an asp.net application could give you quite a hard time. Having  dropped crystal in favor of sql reporting services (so far) just annoying little stuff keeps me busy. In this post I'll demonstrate how to add RS reports to an asp.net application and concentrate on fixing the annoyances.

To the application reports are available as an URL on the report server. You add reports to an asp.net application by hyper-linking to them. Displaying the reports requires authentication and authorization, anonymous access does not work. By default no-one has browsing rights. In the report manger, which is another URL to the report server, you set the browsing rights under the security tab.

Provided the user has the necessary rights the browser will show the report with a viewer toolbar. In the toolbar is the option to export or print the report. Make sure you have installed service pack 2 of reporting services or else the print button will be missing. For the print to work RS will (try to) install a little client side ActiveX control which provides a print preview and a clean print.

A report can have parameters, these are defined in the report's design. You can pass parameter values at runtime provided that the report has the prompt property of its parameters set. If this property is not set you get a nasty reporting services error. There are two way to pass parameters, either in the parameters pane of the report toolbar or in the URL. When you're linking to a report from an application passing the parameters in the URL is  the most likely thing to do. This snippet builds an URL. It does include two parameter values.

string reportserver = System.Configuration.ConfigurationSettings.AppSettings["ReportServer"] + "{0}&rs:Parameters=false&rs:Command=Render&rs:ClearSession=true";
string reportUrl = String.Format("UrenVerdeling&idGebruiker={0}&sort={1}",CurrentUser.ID, sortOn);
Response.Redirect(string.Format(reportServer, reportUrl));
 

To hide the parameter pane in the browser bar you are supposed to pass rs:Parameters=false in the Url. But to hide the pane also the prompt string, asking for a parameter value, should be empty. The prompt string is set in the report designer (in VS)

Now there is a little quirk. In the report definition itself there are two settings for the prompt. A logical one which determines whether to prompt and a string what text to prompt. The report designer lumps these two; an empty string will set the prompt flag to false. Which will result in the exception mentioned. In the report manager you set these two properties independently. To be able to set the parameter from the URL and hide the parameter pane you have to check the prompt checkbox and empty the prompt string textbox.

You don't have to pass in every report parameter. As you see in the report manager there is also a Null column.

When the Null checkbox is checked a parameter can be omitted. Its value will be according to the expression in the Default Value textbox. The VS report designer has several options for that, non-queried, from-query or none. This is again an oversimplification of the underlying parameter properties. To get a default value of NULL for the parameter you have to check the has default and  Null checkboxes as well as empty the Default Value textbox. Also this can only be done right in the report manager.

Now I have a great flexibility and can still keep the URL as simple as possible.

System.Text.StringBuilder sb = new System.Text.StringBuilder("UrenverdelingTotaal");
sb.Append("&CursusJaar=" + CurrentUser.CursusJaarOmschrijving);
sb.Append("&Sortering=" + DropDownListSortering.SelectedIndex.ToString());
sb.Append("&idGebruiker=" + CurrentUser.ID.ToString());
sb.Append("&SelOnderwijs=" + (CheckBoxOnderwijs.Checked ? "1" : "-1"));
sb.Append("&SelOpleiding=" + (CheckBoxOpleiding.Checked ? "1" : "-1"));
sb.Append("&SelSchool=" + (CheckBoxSchool.Checked ? "1" : "-1"));
if (DropDownListSchool.SelectedIndex > 0)
   sb.Append("&Afdeling=" + DropDownListSchool.SelectedValue.Trim());
if (DropDownListOpleiding.SelectedIndex > 0)
   sb.Append("&Opleiding=" + DropDownListOpleiding.SelectedValue.Trim());
if (DropDownListAfkorting.SelectedIndex > 0)
    sb.Append("&Afkorting=" + DropDownListAfkorting.SelectedValue.Trim());
if (DropDownListNaam.SelectedIndex > 0)
    sb.Append("&Naam=" + DropDownListNaam.SelectedValue.Trim());
if (DropDownListKostenplaats.SelectedIndex > 0)
    sb.Append("&KostenPlaats=" + DropDownListKostenplaats.SelectedValue.Trim());
if (DropDownListTelcode.SelectedIndex > 0)
    sb.Append("&TelCode=" + DropDownListTelcode.SelectedValue.Trim());
if (DropdownlistJaargang.SelectedIndex > 0)
    sb.Append("&Jaargang=" + DropdownlistJaargang.SelectedValue.Trim());
if (DropDownListPeriode.SelectedIndex > 0)
   sb.Append("&Periode=" + DropDownListPeriode.SelectedValue.Trim());
if (DropDownListOnderdeel.SelectedIndex > 0)
   sb.Append("&Onderdeel=" + DropDownListOnderdeel.SelectedValue.Trim());
if (TextBoxOpmerking.Text != "")
   sb.Append(string.Format("&Opmerking={0}%", TextBoxOpmerking.Text.Trim()));


Response.Redirect(string.Format(reportServer, sb.ToString()), true);
 

I had found the answer to the prompt problem here in a blogpost whose comments had turned it into just the kind of wiki (loads of comments where the commenters start discussing solutions) some of my old Crystal Reports post have turned into. The solution found  was also applicable to default parameter values. Getting reporting to work (right) is such grateful blog-fodder :)



Comments

K.Senthil Kumar (Sensoft2000) said:

I need to pass "Null" value as a default parameter through the URL without prompting to the user.  I dont know, how to do that. That means, i need to show the report for the particular Stored procedure with paramater 'param1=null' by default.. anybody knows?
# June 15, 2006 9:05 AM

pvanooijen said:

In case null is the default value you can omit it from the url. Have null checked, prompt user checked and leave the prompt string empty. Works for me.
# June 15, 2006 4:15 PM

mark@softtech.nl said:

Hi Peter, thanks for the tip, you helped me out !

Mark

# November 17, 2006 7:20 AM

swapna said:

i integrated reporting services with asp.net page and passing parameters through that page.but when the page is viewed in browser along with the controls in the page a message is displayed " Parameter X missing Value ".

Though after passing parameter value with the help of controls in the page the problem is over but still for the first run it creates problem.........

Plz help me out

# January 16, 2007 6:31 AM

pvanooijen said:

In that case parameter X is missing in the URL for the report

# January 16, 2007 6:44 AM

swapna said:

Thanx Peter

The integration procedure i followed is as given in the article in :

http://www.codeproject.com/sqlrs/ReportViewer2005.asp.

all the steps are same.

I am just calling a report on the button click event of an asp.net page which also gives the parameter value to the report. In report i have allowed null for that parameter.But the problem remains.I am not getting what to do? where to pass parameter in URL is not mentioned in that article.

Can u help me out?

Thanx a lot

# January 16, 2007 10:01 PM

swapna said:

hey my problem is over......

i passed the parameter value as session variable to another page where report is called........its working

Can anybody tell how paging can be enabled in a report?

Thanx

# January 17, 2007 2:59 AM

pvanooijen said:

The report viewer (in the browser) does page by itself. What more do you need ?

# January 17, 2007 4:20 AM

swapna said:

I want to display only a particular number of records in a page so that the user doesn't have to scroll down to view all the records.For e.g when there are 1000 records to be displayed I want to show 10 records per page.

I dont know where to set this property.Do u have any idea?

Thanx.

# January 18, 2007 4:54 AM

pvanooijen said:

AFAIK there is no such property. What you could try is a report band which forces a new page after evry X records.

# January 18, 2007 7:17 AM

suresh said:

only thing u can do is do a grouping

# February 11, 2007 11:26 AM

pvanooijen said:

@suresh: I guess this is a follow up on swapna ?

Looks like an opening. Introduce a "Page" column to group on.

# February 12, 2007 3:35 AM

niks said:

use pagination for displaying required amount of records per page

# February 15, 2007 12:15 AM

swapna(emergencyyyyyy) said:

hi all........

I solved the paging problem by changing the interactive page size in report properties....

Now am having a new problem...till now i was passing parameters to the report by writing code in asp.net.The user entered values in the textboxes in the form(in visual studio )...

         The problem is that now I have to use only Infopath forms and to publish the reports in SharePoint server...I am new to Infopath and sharepoint......and its too urgent.....

can anyone guide me how to do this.... :(......am confused????

# February 25, 2007 9:50 PM

pvanooijen said:

Sorry, not  yet. The moment I have sharepoint Rs experience I'll blog on that. :)

# February 26, 2007 2:24 PM

Ramesh Chandra said:

actually my project is on reporting services

probelm is i ve to pass parameters from a browser(HTML) page so that the reporting services need to take this value as parameters & generate reports based on this parameters

Please kindly help me

Ramesh

9885331312 my contact no:

# March 28, 2007 12:55 AM

pvanooijen said:

creating reports is a matter of using the reporting services webservices. That's a whole book on itself

# March 28, 2007 4:58 AM

pv rao said:

swapna said:

i integrated reporting services with asp.net page and passing parameters through that page.but when the page is viewed in browser along with the controls in the page a message is displayed " Parameter X missing Value ".

Though after passing parameter value with the help of controls in the page the problem is over but still for the first run it creates problem.........

ans:::i passed the parameter value as session variable to another page where report is called........its working

can u help how exactly to pass a session variable to a report

# March 29, 2007 2:20 AM

pvanooijen said:

Invoking a report is a matter of assembling the URL. The parameters are part of that URL. What your code could do is get the session variable and insert that in the right place in the url.

# March 30, 2007 2:13 AM

David Sau said:

I'm still having the same problem ie the print button is not displayed even I've applied the SP2 and re-booted the system (XP).

Your help is most appreciated.

Thanks.

# April 25, 2007 3:22 AM

pvanooijen said:

The print button is handled by an activeX control. Which is installed on the fly by the reporting server.

Provided the policies allow the insatllation of activex

# April 26, 2007 4:37 PM

Sanjay Modi said:

Thanks , its workable

Sanjay Modi

# July 23, 2007 10:01 AM

Raja said:

I am having a problem where the report I'm trying to view via my web app has got cascading parameters. There are three parameters in total. The first parameter is a drop down with a list of options. The second parameter gets populated after the user selects an option from the first.

My problem happens when the user picks an option for the first parameter. The page gets reloaded and that re-loads the report from the beginning. I lose the value picked for the first parameter and it becomes an endless cycle of picking parameter 1 -> page refreshed -> over and over.

Could you please tell me how i can fix this?

# July 26, 2007 1:43 PM

swapna said:

hi all..

sorry  pv rao for so late reply.....

As u have asked how to pass session value for a parameter in report am making it clear over here...

After creating a report use report viewer control to call that report in a .aspx page .From one form pass the parameter value say name entered in one of the textboxes of the form.Then retrieve this name as session value in the page where u r using report viewer control.Then set the parameter value to this session value for which u can refer the article

www.codeproject.com/.../ReportViewer2005.asp

Hope it gets clear now...

# August 10, 2007 6:24 AM

Chandu said:

I want to display a data in pdf file in sql reporting services...Pls tell me the solution.?

# September 13, 2007 7:42 AM

pvanooijen said:

RTFaspx It's right there under your nose in the report viewer.....

# September 13, 2007 8:21 AM

Desmond said:

Hi all. I'm new to reporting services. I've done the basic and now, i need to know how to pass parameters from applic. to reporting services.

The integration procedure i followed is as given in the article in :

www.codeproject.com/.../ReportViewer2005.asp.

However, i'm unable to know my "ReportPath" and my "ReportServerUrl". Anyone can help me out?

Feel free to email me at: juzshaddup@hotmail.com

Thanks for your time.

# September 14, 2007 4:49 AM

Krishni said:

Hi there

I have a few reports on report manager which have drill throughs to other reports. The drill throughs are done via URLs, and when the reports are exported to excel, the drill through and is still available together with the URL. is there a way for me to hide the URL and drill thoughs when exporting reports?

ta

Krishni

# October 8, 2007 5:54 AM

swapana said:

hii..

Has anybody got idea about Report Builder.Is there any way to launch it in our custom application so that it opens in own application's web form and not in a separate window..

Any help is appreciated...

# October 15, 2007 5:13 AM

pvanooijen said:

Whether it open in a new browser or in the webform of your app is your choice. It's just an url like any other. You could even open it inside a frame (yuck..) of you app..

# October 15, 2007 2:55 PM

Swapana said:

Thanks a lot  :-)

AFIK the report builder is launched in a separate window the moment we enter the report builder's URL...

Can u suggest how to open the report builder designer window in our own application's web form(may be inside a frame) instead of opening it in the default SQL server report builder application window.

# October 19, 2007 6:26 AM

Priya said:

Hi,

i built a report with SSRS 2005, having multivalued parameter. now i need to show a message box whn the user selects more than two values in it.

how do i implement this?

kindly help

tia.

# October 26, 2007 2:52 AM

Ishraque said:

Nice comment, it really helped me and my team immensly in our project, keep up the nice work!!

# June 13, 2008 9:42 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add
Check out Devlicio.us!

This Blog

Syndication

News