C# – Passing a Parameter to ReportView

asp.netcparametersreporting-services

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.

Best Answer

You can use ServerReport.SetParameters:

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

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