Wednesday, March 28, 2012

ReportViewer Component

Hi all,

I'm creating a custom interface for reporting services but I am having a few problems. This is my environment:

Using the ReportViewer component in remote mode to show my reports of the report server. There are 2 user levels of which any user can be apart of, Admin & user. In every report, there is a parameter called MERCHANTID, when the user who logs in (via custom login interface) is of group admin, then the merchantid parameter gets prompted before the report is run, if the group of the user is "user" then the merchantid parameter is hidden, and is passed programatically to the report. The report path also

My Questions:

1. When my reportviewer component loads up the report and prompts for a parameter, when you select a parameter value or type it in, it just posts back with no results, just the parameter prompt again with no data... How can this be corrected?

2. How do I pass report parameters programatically... so that when my user id of group "user" then "merchantid" gets passed automatically, but if there is any more parameters, then that gets prompted...

Thanks for your help.

For issue #1: You are probably setting the report path or server url after the LoadViewState event, most likely during load. When you set this value, the viewer sees it as defining a new report. So it ignores input from the client, thinking it applies to the old report. You should be setting this information during Init or only if it is the GET request (!Page.IsPostback), depending on your architecture.

For issue #2: You can programmatically set the parameter value using ReportViewer.ServerReport.SetParameters(). Each ReportParameter object you pass in has a Visible property on it. If you set this to false when setting the report parameter, the client will not see a prompt for that parameter.

|||

Hey man,

Thanks alot for the response, I tried moving my code to the init code, and that worked great.... could you help me with the 2nd part though? This is what I need to do:

If Userlevel = 1 then the report parameter called "MERCHANTID" (If the report has it)needs to be visible for prompting. Else, "MERCHANTID" is hidden and the value session.item("MerchantID") is passed as the value.

This is the code I'm currently using, but not working.

Dim Param As ReportParameter = New ReportParameter
If Not UserLevel = 1 Then
If Param.Name = "MERCHANTID" Then
Param.PromptUser = False
Param.Equals(Session.Item("MerchantID"))
End If
Else
Param.PromptUser = True
Param.DefaultValues = Session.Item("MerchantID")
End If
ReportViewer1.ServerReport.SetParameters(Param)

And this is the error I'm getting... Line 54 is the last line in the example I'm giving.

System.InvalidCastException: Unable to cast object of type 'ReportServer2005.ReportParameter' to type 'System.Collections.Generic.IEnumerable`1[Microsoft.Reporting.WebForms.ReportParameter]'. at Report.Page_Init(Object sender, EventArgs e) in \\Nccptws001\wwwroot\NetReports\Report.aspx.vb:line 54

Thanks

|||

The compiler is complaining about the last line. SetParameters takes an array of ReportParameter objects, not a single one.

But there are many other problems with your code. Param.Name is always empty string until you set it, so it will never equal "MerchantId". ReportParameter doesn't have a PromptUser or DefaultValues property. The property to set for visibility is Visible, not PromptUser. The property to set the value is Values not DefaultValues. See this help page for more information about ReportParameter: http://msdn2.microsoft.com/en-us/library/microsoft.reporting.webforms.reportparameter_properties(VS.80).aspx

|||

Hi Brian,

Sorry for besing such a wuss, but could you help me with the code to achieve this? How could I set the merchantid field and actually first check if the merchantid parameter does exist on this report?

Thanks Brian.

Rudi Groenewald

|||

Hi Brian,

I'm trying the following code, but no luck yet:

ReportViewer1.ProcessingMode = Microsoft.Reporting.WebForms.ProcessingMode.Remote
ReportViewer1.ServerReport.ReportServerUrl = New Uri(Http://REPORTSERVER/Reportserver)
ReportViewer1.ServerReport.ReportPath = ReportPath

'SET THE REPORT PARAMETERS
Dim instance As New Microsoft.Reporting.WebForms.ReportParameter
If Not UserLevel = 1 Then
instance.Name = "MERCHANTID"
instance.Values.Add(Session.Item("merchantID"))
instance.Visible = False
Else
instance.Name = "MERCHANTID"
instance.Values.Add(Session.Item("merchantID"))
instance.Visible = True
End If
ReportViewer1.ServerReport.SetParameters(instance)

And I'm getting the error:

System.InvalidCastException: Unable to cast object of type 'Microsoft.Reporting.WebForms.ReportParameter' to type 'System.Collections.Generic.IEnumerable`1[Microsoft.Reporting.WebForms.ReportParameter]'. at Report.Page_Init(Object sender, EventArgs e) in \\Nccptws001\wwwroot\NetReports\Report.aspx.vb:line 58

It's still moaning at the last line.

and I still don't want to pass the "MerchantID" parameter if the report doesn't have report parameters.... Any ideas?

Thanks alot for the help.

Regards,

Rudi Groenewald

|||The rest of your code looks good now, but you are still passing in a single instance of ReportParameter to SetParameters. The method takes an array of ReportParameter.|||

Hi Brian,

ok.. but that's the only parameter I want to pass... I don't quite understand what you're saying....

|||The method takes an IEnumerable. If you want to pass a single report parameter, you still need to pass an array of one element.|||

Hi Brian...

Ok, Thanks, I understand now what I should do... but how do I do it?

Could you give me some sample code?

Thanks

|||This should work

'SET THE REPORT PARAMETERS
Dim instance As New Microsoft.Reporting.WebForms.ReportParameter
...
dim prms(0) as Microsoft.Reporting.WebForms.ReportParameter
prms(0) = instance

ReportViewer1.ServerReport.SetParameters(prms)sql

No comments:

Post a Comment