Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
196 views
in Technique[技术] by (71.8m points)

c# - Passing a Parameter to ReportView

I have the following code:

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;
}

Quite simply, every time a report is selected from my dropdown list ddlCompanyList_SelectedIndexChanged will run which just uses a stored procedure to find the URL of the report selected from the drop down. This will then be passed onto GetReportPath which will pass the reports URL into the ReportView.

Everything works fine so far, however I have decided to add CustomerID as a parameter in my report on SSRS. Passing parameters into a stored procedure was easy, however I'm a bit lost when it comes to passing a parameter (CustomerID) into the report itself. Since strCustomerID is automatically populated with the customer's ID thanks to a code I wrote, it would be great if I could just pass strCustomerID as the parameter for my report, so that users don't have to manually enter their own ID.

enter image description here

Anyone have any suggestions? Thanks for the help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use ServerReport.SetParameters:

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

this.reportViewer1.ServerReport.SetParameters(new ReportParameter[] { p });

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...