Asked by:
How to pass parameters to WebForms ReportViewer and its LocalReport during ASP.NET page load?

Question
-
Owing to the ReportViewer being incompatible with AJAX, I need to write a small non-AJAX page in my web application to host the ReportViewer. When this page loads, the local report successfully queries my custom 'business objects' class to load its dataset - as part of that load function, I also need to pass a couple of 'out of band' parameters into the report that are calculated during the data load process (e.g. correlation coefficient).
The problem is that by the time the data source is queried, the reportviewer is in a 'readonly' state (the specific exception I get is: "Viewer control object is in a read only state" when I call SetParameters() within the data source load function).
So, is there any way I can pass these data characteristics into the report?Friday, November 6, 2009 1:16 PM
All replies
-
Try this
Public Sub ProjectionReport() 'Set the Processing mode for the ReportViewer to Remote ReportsForm.ReportViewer1.ProcessingMode = ProcessingMode.Remote Dim ServerReport As ServerReport ServerReport = ReportsForm.ReportViewer1.ServerReport 'Get a reference to the default Credentials Dim Credentials As System.Net.ICredentials Credentials = System.Net.CredentialCache.DefaultCredentials 'Get a reference to the report server credentials Dim rsCredentials As ReportServerCredentials rsCredentials = ServerReport.ReportServerCredentials 'Set the report server URL and Report Path ServerReport.ReportServerUrl = _ New Uri("http://myServer/ihreportserver") ServerReport.ReportPath = _ "/Reports/HotelsForecastAnalysisTabular" 'Create the report parameters for the report Dim myAccessCode As String = MainForm.ToolStripLabelAccessCode.Text Dim ParamAccessCode As New ReportParameter() ParamAccessCode.Name = "AccessCode" ParamAccessCode.Values.Add(myAccessCode) 'Set the report Parameters for the report Dim Parameters() As ReportParameter = {ParamAccessCode} ServerReport.SetParameters(Parameters) 'Refresh the report ReportsForm.ReportViewer1.RefreshReport() End Sub
JohnFriday, November 6, 2009 3:06 PM -
Thanks John. A couple of follow-up questions, however:
- I'm using LocalReports, not ServerReports, but I can translate the intent. THat said, at what point would I execute such code? As I mentioned, I don't have access to the value for the parameter until after the report has called out to its data source to load the data, at which time it appears to be in a 'read-only' state?
THanks,
ANdyFriday, November 6, 2009 5:43 PM -
This code runs when the form load, if your report is local (from the application) there are tons of events on the report viewer control so you can take advantage of.
I'm not sure I really understand this part : "As I mentioned, I don't have access to the value for the parameter until after the report has called out to its data source to load the data" Can you please elaborate
Who's supplying the parameter? User or Code?
John- Edited by Codernater Friday, November 6, 2009 8:29 PM elaborate
Friday, November 6, 2009 8:29 PM -
The report viewer is configured with an ObjectDataSource, which in turn refers to a custom class that extracts a data set from a database. As part of that extraction, it runs a simple linear regression calculation over a couple of columns in every row, at the end of which it can calculate the correlation coefficient. I want to pass that correlation coefficient into the report.
I've tried hooking every event imaginable on the ReportViewer (and its underlying Control base class) - the early events fire before the report binds to its data source; the later events appear to happen after the report is in a 'read only' state. I've even passed a reference to the ReportViewer control to the business object data load routine (via session state) so that I can attempt to call SetParameters() on the report as soon as I know the correlation coefficient i.e. as soon as I know its value, but even then I get the 'read-only' exception...
Cheers,
AndyFriday, November 6, 2009 11:40 PM -
hi aclcarter,
please find the below code this is really working fine,
Dim RptParam1(0) As ReportParameter
'Setting Name to Report Parameter
' Parameter Name defined here should be exactly similar to name defined in ' Report
RptParam1(0) = New ReportParameter("RptParam1")
'Assigning Values to Parameter
RptParam1(0).Values.Add(“Test Value”)
'Finally Add The Parameter To Report Viewer
reportViewer1.LocalReport.SetParameters(RptParam1)
try to use this, no need for you to set processing mode to remote
Hope this helps
-Raja Sekhar K- Proposed as answer by RajaSekhar-Navigator Monday, November 9, 2009 4:45 AM
- Unproposed as answer by aclcarter Monday, November 9, 2009 11:54 AM
Monday, November 9, 2009 4:45 AM -
Hi Raja,
Unfortunately this does not work. Because the ReportViewer is the only control on the page, configured declaratively with an object data source, I am struggling to find an opportunity to call "SetParameters()" during the page load cycle where the ReportViewer has not put itself into a 'read-only' state, but has call the data source to load the data.
Thanks,
AndyMonday, November 9, 2009 11:58 AM -
This is an old thread but I am having a similar issue.
Does anybody know how I can do that.
I can set the report parameter after the objectdatasource loads the data.
Wednesday, November 13, 2013 7:02 PM