LunaCrescens
LunaCrescens

Reputation: 540

How to pass parameters to SSRS report programmatically

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.

Upvotes: 15

Views: 57304

Answers (4)

Abram Simon
Abram Simon

Reputation: 3269

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

Upvotes: 15

girish.M
girish.M

Reputation: 19

ReportViewer1.LocalReport.DataSources.Clear();
ReportViewer1.Reset();
Label1.Visible = false;
ReportViewer1.Visible = true;
DataSet dataSet = new DataSet();
dataSet = new ClassBLL().Load_Report_Detail(TextBox1.Text, 
ddlType.SelectedValue, levelcode, fields);
ReportDataSource datasource = new ReportDataSource("DataSet_StoreprocedureName",
dataSet.Tables[0]);

if (dataSet.Tables[0].Rows.Count == 0)
{
    ReportViewer1.Visible = false;
}

ReportViewer1.LocalReport.ReportPath = Server.MapPath("") + @"\Report.rdlc";
ReportViewer1.LocalReport.DataSources.Clear();
ReportViewer1.LocalReport.DataSources.Add(datasource);
string fields="name,girish,Z0117";
string[] filedName = fields.Split(',');
ReportParameter[] param = new ReportParameter[2];

//for (int i = 0; i < filedName.Length; i++)
//{

param[0] = new ReportParameter(filedName[0], filedName[0], true);
param[1] = new ReportParameter(filedName[3], filedName[3], true);

// }


ReportViewer1.LocalReport.SetParameters(param);

ReportViewer1.ServerReport.Refresh();

Upvotes: 1

LenexaKS
LenexaKS

Reputation:

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

Upvotes: 1

HectorMac
HectorMac

Reputation: 6143

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

Upvotes: 11

Related Questions