Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

Call SSRS Reports by using C#

4.60/5 (12 votes)
29 Oct 2013CPOL2 min read 223.4K   3  
Call/Run SSRS Reports by using C#

Introduction

Sometimes user are creating SSRS Reports and want to Run that SSRS Report by using their Web Appliation. Here I am describing how to Call a SSRS Report by using C#.

I am here assuming that User know how to create SSRS Report. So here I am only describing to Call SSRS Report by using C#.

Background

What is the need to call SSRS Report using C#?

SSRS also provides a 'Report Builder' tool for less technical IT workers to format SQL reports of lesser complexity. After Creating of SSRS Reports for the business need, most of the user want to view it on Website or WebApplication by using different languages.

Using the code

Steps Used for Call SSRS Report are : -

  • Create a ReportPage.aspx page having ReportViewer Control, named : - rptViewer
  • In ReportPage.aspx.cs, Add Reference of Microsoft.Reporting.WebForms
  • It will be easily found into the location: - c:\Program Files (x86)\Microsoft Visual Studio 10.0\ReportViewer\Microsoft.ReportViewer.WebForms.dll
  • Set the Processing Mode to Remote
  • Pass the Report URL and Report Path
  • If you pass the Parameter then Pass that Parameter
  • If User want to pass Credential of Report Server, then by using ReportServerCredentials Credential will be passed
  • Call the SSRS Report

Code to Call SSRS Report

C#
private void ShowReport()
{
    try
    {
        string urlReportServer = "http://sqlDBServer//Reportserver";
        rptViewer.ProcessingMode = ProcessingMode.Remote; // ProcessingMode will be Either Remote or Local
        rptViewer.ServerReport.ReportServerUrl = new Uri(urlReportServer); //Set the ReportServer Url
        rptViewer.ServerReport.ReportPath = "/ReportName"; //Passing the Report Path                

        //Creating an ArrayList for combine the Parameters which will be passed into SSRS Report
        ArrayList reportParam = new ArrayList();
        reportParam = ReportDefaultPatam();

        ReportParameter[] param = new ReportParameter[reportParam.Count];
        for (int k = 0; k < reportParam.Count; k++)
        {
            param[k] = (ReportParameter)reportParam[k];
        }
        // pass crendentitilas
        //rptViewer.ServerReport.ReportServerCredentials = 
        //  new ReportServerCredentials("uName", "PassWORD", "doMain");

        //pass parmeters to report
        rptViewer.ServerReport.SetParameters(param); //Set Report Parameters
        rptViewer.ServerReport.Refresh();
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

On the above function ReportDefaultPatam function is used to Set the Default Parameters for the SSRS Report.

C#
reportParam = ReportDefaultPatam();

We are fetching the Report Parameters into ArrayList.

For every parameter of ArrayList, we are adding them into ReportParameter array.

Collection of ReportParameter array is set into the Report by using the below code : -

C#
rptViewer.ServerReport.SetParameters(param);

In the ReportDefaultPatam function, we are adding SSRS Report Parameters into the Array List

C#
private ArrayList ReportDefaultPatam()
{
    ArrayList arrLstDefaultParam = new ArrayList();
    arrLstDefaultParam.Add(CreateReportParameter("ReportTitle", "Title of Report"));
    arrLstDefaultParam.Add(CreateReportParameter("ReportSubTitle", "Sub Title of Report"));
    return arrLstDefaultParam;
}

On the above function we are using CreateReportParameter function. In this function we are returning the ReportParameter type having the information of Parameter Name and Parameter Value.

C#
 private ReportParameter CreateReportParameter(string paramName, string pramValue)
{
    ReportParameter aParam = new ReportParameter(paramName, pramValue);
    return aParam;
}

Points of Interest

I was trying to Call the SSRS Report by using C#. For that, I had found the simplest ways, Which I described here. I had also Called the SSIS Package by using C# and SQL Server. Which I described into my another article. These articles are:

History

  • 29th October, 2013: Initial post.

License

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