Setting SSRS Report Parameters from ASP.NET C# Code

It is sometimes necessary to customise SSRS (SQL Server Reporting Services) reports at runtime and one way of achieving this is through the use of report parameters. One reason for doing this might be to customise the report title, column headers, currency, number formats and date formats based on the user’s language settings.

This article describes how to ‘push’ the data into the report which may not always be desirable. Pushing the data into a report is suitable for a report that is hosted inside a web page, but is not appropriate if the report will be run directly from the SSRS Report Manager, in those circumstances the report will need to ‘pull’ the data into itself using an external library or rely on default values.

This technique works equally well with both server reports and local reports.

Method

Add one parameter for each value you wish to customise in the report; e.g. a report title, each report column header, the date format to use. If it is a server report the parameter should be marked as Hidden and I also recommend that you give each parameter a default value so that you can still run the report from the Report Manager and can Preview the report to help with its design.

Change each value so that it uses the relevant parameter value instead. For instance:

  • The parameter for the Surname column’s header might be named SurnameColumnHeader, in this case the header for that column should be set to an expression of =Parameters!SurnameColumnHeader.Value
  • The parameter for the date formnt might be named DateFormat, in this case the format for the Date of Birth columns data should be set to an expression of =Parameters!DateFormat.Value

Add code to set the parameter values at runtime, this might be in the Page_Load or perhaps when the user clicks on a button to request the report, for example:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        List<ReportParameter> paramList = new List();
        paramList.Add(new ReportParameter("SurnameColumnHeader", 
            GetGlobalResourceObject("UserReport", "SurnameColumn").ToString()));
        ReportViewer1.LocalReport.SetParameters(paramList);
    }
}

Posted

in

by

Tags: