Click here to Skip to main content
15,886,137 members
Articles / Web Development / ASP.NET

Call SSRS Reports by using C#

Rate me:
Please Sign up or sign in to vote.
4.60/5 (12 votes)
29 Oct 2013CPOL2 min read 220.3K   43   10
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)


Written By
Software Developer (Senior) R1 RCM Pvt. Ltd.
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRegarding Item path Pin
Member 149302722-Mar-21 2:06
Member 149302722-Mar-21 2:06 
QuestionWhere to call ShowReport() ? Pin
Divyapalivela10-Jun-19 23:14
Divyapalivela10-Jun-19 23:14 
AnswerRe: Where to call ShowReport() ? Pin
Sandeep Kumar Tripathi6-Aug-19 1:36
Sandeep Kumar Tripathi6-Aug-19 1:36 
QuestionConfiguration of SSRS Pin
sirch fsarso17-Dec-14 19:50
sirch fsarso17-Dec-14 19:50 
Question"The request failed with HTTP status 401: Unauthorized." You can Heal me ? Pin
Member 108076119-May-14 11:01
Member 108076119-May-14 11:01 
QuestionProblem with SSRS Authentica​tion - don't want to use Custom Authentica​tion - Please guide Pin
Member 1069685125-Mar-14 4:20
Member 1069685125-Mar-14 4:20 
QuestionHow to make SSRS report to accept parameters from C# Pin
Member 1067755917-Mar-14 11:57
Member 1067755917-Mar-14 11:57 
QuestionHow to download this sample code Pin
Member 191357527-Nov-13 17:28
Member 191357527-Nov-13 17:28 
SuggestionArrayList Pin
Richard Deeming8-Nov-13 8:02
mveRichard Deeming8-Nov-13 8:02 
GeneralGood Article Pin
Gehan Fernando3-Nov-13 22:18
Gehan Fernando3-Nov-13 22:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.