开发者

How to pass parameters in SSRS

开发者 https://www.devze.com 2023-04-06 14:35 出处:网络
I\'m pretty new to SSRS and have got stuck on passing parameters. Currently I\'m doing the following:

I'm pretty new to SSRS and have got stuck on passing parameters. Currently I'm doing the following:

ReportViewer1.ServerReport.ReportPath = ConfigurationManager.AppSettings["serverPath"] + "MyReport";
List<ReportParameter> paralist = new List<ReportParameter>();                
ReportParameter reportParam1 = new ReportParameter("LocationId", txtLocationId.Text);
ReportParameter reportParam2 = new ReportParameter("PrdAcctId", txtProductAccountId.Text);
ReportParameter reportParam3 = new ReportParameter("FromDate", frmdt);
ReportParameter reportParam4 = new ReportParameter("ToDate", todt);
ReportParameter reportParam5 = new ReportParameter("IntA开发者_JAVA技巧mt", todt);
paralist.Add(reportParam1);
paralist.Add(reportParam2);
paralist.Add(reportParam3);
paralist.Add(reportParam4);
paralist.Add(reportParam5);
ReportViewer1.ServerReport.SetParameters(paralist);

This works fine when the no. of parameters are set. But in some cases the no. of parameters will be decided at run time based on what user inputs.

Now I'm at a loss on how to make the no. of parameters flexible in the .rdl file. For example there can be n number of FromDate and ToDate and corresponding IntAmt as per user input.

I'd appreciate any help on this and Thanks in advance.


You can call the GetParameters method to get all the current report parameters and loop through them to see what each is and the various properties of each:

Start with:

ReportParameterInfoCollection param = ReportViewer1.ServerReport.GetParameters();


You can try the following, it works - replace index 0 with the ordinal value of your parameter:

if (this.rptView.ServerReport.GetParameters()[0].Values.Count > 0)
{
    string parvalue = this.rptView.ServerReport.GetParameters()[0].Values.ToString();
    paramList.Add(new ReportParameter("ReportName", parvalue));
}


This is not a direct answer to your question. Our main page redirects to the report rendering page using window.href and passes the parameters as query string.

Note: This is VB.Net code which you can convert to equivalent C# code

CODE BEHIND

Public Function GetParametersList(ByVal reportRDLName As String) As List(Of String)
    'Get these values from database. Following is a sample
    Dim parametersList As New List(Of String)()
    parametersList.Add("plantcd")
    parametersList.Add("CountID")
    Return parametersList
End Function

Public Function GetReportPath(ByVal reportRDLName As String) As String
    'Get these values from database. Following is a sample
    Dim reportPath As String = "/MyFolder/MyModule/"
    Return reportPath
End Function

Protected Overrides Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)

    '--------------------------------------------------------
    Me.EnableViewState = True

    If Not Page.IsPostBack Then

        vwreports.ServerReport.ReportServerCredentials = New MyCustomReportServerCredentials
        vwreports.ProcessingMode = Microsoft.Reporting.WebForms.ProcessingMode.Remote
        vwreports.ServerReport.ReportServerUrl = New Uri(ConfigurationManager.AppSettings("MyReportServer"))



        Dim reportRDLName As String = Request.QueryString("rname").ToString()
        Dim parametersList As List(Of String) = GetParametersList(reportRDLName)
        Dim reportPath As String = GetReportPath(reportRDLName)

        Dim countOfParameters As Integer = parametersList.Count
        Dim parms As ReportParameter() = New ReportParameter(countOfParameters - 1) {}
        Dim iterationCount As Integer = 0

        For Each parameterName As String In parametersList
            Dim parameterValue As String = Convert.ToString(Request.QueryString(parameterName))
            If Not [String].IsNullOrEmpty(parameterValue) Then
                parms(iterationCount) = New ReportParameter(parameterName, parameterValue)
                iterationCount += 1
            End If
        Next

        vwreports.ServerReport.ReportPath = reportPath + reportRDLName
        vwreports.ServerReport.SetParameters(parms)
        vwreports.ShowParameterPrompts = False
        vwreports.ServerReport.Refresh()


    End If
End Sub
0

精彩评论

暂无评论...
验证码 换一张
取 消