How to pass parameters to SSRS report programmatically How to pass parameters to SSRS report programmatically asp.net asp.net

How to pass parameters to SSRS report programmatically


You can do the following,: (it works both on local reports as in Full Blown SSRS reports. but in full mode, use the appropriate class, the parameter part remains the same)

LocalReport myReport = new LocalReport();myReport.ReportPath = Server.MapPath("~/Path/To/Report.rdlc");ReportParameter myParam = new ReportParameter("ParamName", "ParamValue");myReport.SetParameters(new ReportParameter[] { myParam });// more code here to render report


If the Report server is directly accessible, you can pass parameters in the Querystring if you are accessing the repoort with a URL:

http://MyServer/ReportServer/?MyReport&rs:Command=Render&Param1=54321&Param2=product

You can add output formatting by adding the following on the end of the URL:

&rs:Format=Excel

or

&rs:Format=PDF


It's been a while since I did this code, but it may help:Your web project has to be a Web Site, and not a project of type "ASP.Net Web Application", or you won't be able to add the reference mentioned below.Right click on the project and add an ASP.Net folder - App_WebReferences. You'll have to specify the server where your SRS is; choose the .asmx.Once it's added, the folder under that level is called RSService, and under that are 2 things: reportservice.discomap & .wsdl.In my VB, I do Imports RSService and Imports System.Web.Services.Protocols, then...

Dim MyRS As New ReportingService

The reporting service is on a different server than the webserver the app is on, so I can't do the following: MyRS.Credentials = System.Net.CredentialCache.DefaultCredentials

Instead: MyRS.Credentials = New System.Net.NetworkCredential(rs1, rs2, rs3),where the rs1/2/3 are the login to SRS box, password to SRS box, & domain name". (These are encrypted in my web.config.)

Then, a mass-paste:

MyRS.Credentials = New System.Net.NetworkCredential(rs1, rs2, rs3)Dim ReportByteArray As Byte() = NothingDim ReportPath As String = "/SRSSiteSubFolder/ReportNameWithoutRDLExtension"Dim ReportFormat As String = "PDF"Dim HistoryID As String = NothingDim DevInfo As String = "<DeviceInfo><Toolbar>False</Toolbar></DeviceInfo>"'Dim x As ReportParameter - not necessaryDim ReportParams(0) As ParameterValueReportParams(0) = New ParameterValue()ReportParams(0).Name = "TheParamName"ReportParams(0).Value = WhateverValueDim Credentials As DataSourceCredentials() = NothingDim ShowHideToggle As String = NothingDim Encoding As StringDim MimeType As StringDim ReportHistoryParameters As ParameterValue() = NothingDim Warnings As Warning() = NothingDim StreamIDs As String() = Nothing'Dim sh As New SessionHeader() - not necessary''MyRS.SessionHeaderValue = sh - not necessaryReportByteArray = MyRS.Render(ReportPath, ReportFormat, HistoryID, DevInfo, ReportParams, Credentials, _    ShowHideToggle, Encoding, MimeType, ReportHistoryParameters, Warnings, StreamIDs)'(Yay! That line was giving "HTTP error 401 - Unauthorized", until I set the credentials' as above, as explained by http://www.odetocode.com/Articles/216.aspx.)'Write the contents of the report to a PDF file:Dim fs As FileStream = File.Create(FullReportPath, ReportByteArray.Length)fs.Write(ReportByteArray, 0, ReportByteArray.Length)fs.Close()Call EmailTheReport(FullReportPath)If IO.File.Exists(FullReportPath) Then    IO.File.Delete(FullReportPath)End If