views:

11940

answers:

4

I'm looking for a little help on programmatically passing parameters to a SSRS report via VB.NET and ASP.NET. This seems like it should be a relatively simple thing to do, but I haven't had much luck finding help on this.

Does anyone have any suggestions on where to go to get help with this, or perhaps even some sample code?

Thanks.

+6  A: 

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

HectorMac
Passing the parameters as a URL seems like it opens the door for someone to hack your report. This might be okay for an internal site, but probably not for a public facing site.
+7  A: 

If you are using local report generation instead of a full blown report server, you can do the following:

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
AJ
A: 

Could you detail how to add that to the URl. The rport server is accessed using the report server url and the report path .In which part we need to mention this.I'm getting rsInvaliditemPath error saying that the path should be 260 characters long... Any solution to this problem

A: 

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() = Nothing
            Dim ReportPath As String = "/SRSSiteSubFolder/ReportNameWithoutRDLExtension"
            Dim ReportFormat As String = "PDF"
            Dim HistoryID As String = Nothing
            Dim DevInfo As String = "<DeviceInfo><Toolbar>False</Toolbar></DeviceInfo>"
            'Dim x As ReportParameter - not necessary
            Dim ReportParams(0) As ParameterValue
            ReportParams(0) = New ParameterValue()
            ReportParams(0).Name = "TheParamName"
            ReportParams(0).Value = WhateverValue

            Dim Credentials As DataSourceCredentials() = Nothing
            Dim ShowHideToggle As String = Nothing
            Dim Encoding As String
            Dim MimeType As String
            Dim ReportHistoryParameters As ParameterValue() = Nothing
            Dim Warnings As Warning() = Nothing
            Dim StreamIDs As String() = Nothing
            'Dim sh As New SessionHeader() - not necessary
            ''MyRS.SessionHeaderValue = sh - not necessary

            ReportByteArray = 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