将参数传递给 ReportView

Jim*_*myK -1 c# asp.net parameters reporting-services

我有以下代码:

protected void ddlCompanyList_SelectedIndexChanged(object sender, EventArgs e)
{

    string strConnectionString = ConfigurationManager.ConnectionStrings[Constants.ConnectionStringName].ConnectionString;
    string strCustomerID = CommonCode.GetCurrentCustomerID();
    string strUserID = CommonCode.GetCurrentUserID().ToString();
    DropDownList ddl = sender as DropDownList;
    string strRSReportLinkID = ddl.SelectedValue;
    using (SqlConnection connection = new SqlConnection(strConnectionString))

    {
        connection.Open();
        SqlCommand command = new SqlCommand("Test_GetReportSettingsForReport", connection);
        command.CommandType = CommandType.StoredProcedure;
        command.Parameters.AddWithValue("@CustomerID", strCustomerID);
        command.Parameters.AddWithValue("@UserId", strUserID);
        command.Parameters.AddWithValue("@RSReportLinkID", strRSReportLinkID);

        SqlDataReader reader = command.ExecuteReader();

        if (reader.HasRows)
        {
            while (reader.Read())
            {
                string strURL = reader.GetValue(3).ToString();
                GetReportPath(strURL);

            }
        }

        else
        {
           /* Console.WriteLine("No rows found.");*/
        }

        reader.Close();
        connection.Close();
    }

}

protected void GetReportPath(string strURL)
{
    this.ReportViewer1.ServerReport.ReportPath = "/" + strURL;
}
Run Code Online (Sandbox Code Playgroud)

很简单,每次从我的下拉列表中选择报告时,ddlCompanyList_SelectedIndexChanged 都会运行,它仅使用存储过程来查找从下拉列表中选择的报告的 URL。然后将其传递到 GetReportPath,后者将报告 URL 传递到 ReportView。

到目前为止一切正常,但是我决定在我的 SSRS 报告中添加 CustomerID 作为参数。将参数传递到存储过程很容易,但是在将参数 (CustomerID) 传递到报表本身时,我有点迷茫。由于我编写的代码会自动使用客户的 ID 填充 strCustomerID,因此如果我可以将 strCustomerID 作为我的报告的参数传递,这样用户就不必手动输入他们自己的 ID,那就太好了。

在此处输入图片说明

有人有什么建议吗?谢谢您的帮助。

tez*_*zzo 5

您可以使用ServerReport.SetParameters

ReportParameter p = new ReportParameter("CustomerID", strCustomerID);

this.reportViewer1.ServerReport.SetParameters(new ReportParameter[] { p });
Run Code Online (Sandbox Code Playgroud)