Click here to Skip to main content
15,888,401 members
Articles / Web Development / ASP.NET
Tip/Trick

SSRS Date Parameter Format

Rate me:
Please Sign up or sign in to vote.
4.95/5 (4 votes)
27 Feb 2017CPOL1 min read 22.2K   4  
Set the format of a SSRS date parameter apart from the SSRS report localization

Introduction

Let's say that you have a SSRS report and it has a date parameter. When this report is being generated with Microsoft.Reporting.WebForms.ReportViewer, the format of the date parameter text box is defined by the language of the report (en-US, de-DE, ...). You can find the Language property under the Localization group of the Report properties.

This SSRS report is used by users from different countries and you would like to format the date picker in accordance with the user date format localization. The user localization can be an OS localization or a database-defined localization. The problem is that you cannot set the format of the SSRS parameters individually. They always take up the localization of the report. Here's a solution for this problem.

The page markup has a ReportViewer control with ProcessingMode set to Remote.

ASP.NET
<rsweb:ReportViewer ID="ReportViewer1" runat="server" 
    ProcessingMode="Remote" 
    ShowParameterPrompts="true" 
    SizeToReportContent="true" 
    Style="display: table !important; margin: 0px; overflow: auto !important;">
</rsweb:ReportViewer>

The trick is to set the thread culture when the page is loaded and pass a generic date format to the report parameter.

C#
protected void Page_Load(object sender, EventArgs e)
{
    string userCultureName = "en-US";
    var userCulture = System.Globalization.CultureInfo.CreateSpecificCulture(userCultureName);
    System.Threading.Thread.CurrentThread.CurrentCulture = userCulture;

    if (!IsPostBack)
    {
        ReportViewer1.ServerReport.ReportPath = "/ReportName";
        ReportViewer1.ServerReport.ReportServerUrl = new Uri("http://ServerAddress/ReportServer");
        ReportViewer1.ServerReport.ReportServerCredentials = ReportServerCredentials.Credentials;

        DateTime date = DateTime.Now.ToString("yyyy\\-MM\\-dd HH\\:mm\\:ss");

        ReportViewer1.ServerReport.SetParameters(
            new Microsoft.Reporting.WebForms.ReportParameter[] {
                new Microsoft.Reporting.WebForms.ReportParameter("Date", date, true),
            }
        );

        ReportViewer1.ServerReport.Refresh();
    }
}

When the Thread.CurrentThread.CurrentCulture is changed, the report UI will change accordingly but the report itself won't because it is being rendered on the reporting server, not the IIS, where the report Language property governs the report localization.

The value of the date parameter is formated to "yyyy\-MM\-dd HH\:mm\:ss". First, the format yyyy-MM-dd HH:mm:ss ensures that no matter what the culture is, the reporting server always knows how to parse the date string to the correct date. Second, the backslash before the date & time separators ensures that they are not replaced with the culture's date & time separators, which are defined in the CultureInfo that we set as the thread's CurrentCulture.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Israel Israel
https://github.com/yuvalsol

Comments and Discussions

 
-- There are no messages in this forum --