Click here to Skip to main content
Click here to Skip to main content

Tagged as

Call SSRS Reports by using C#

, 29 Oct 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
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

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.

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 : -

rptViewer.ServerReport.SetParameters(param);

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

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.

 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)

Share

About the Author

Sandeep Kumar Tripathi
Software Developer Accretive Health
India India
No Biography provided

Comments and Discussions

 
Question"The request failed with HTTP status 401: Unauthorized." You can Heal me ? PinmemberMember 108076119-May-14 12:01 
QuestionProblem with SSRS Authentica​tion - don't want to use Custom Authentica​tion - Please guide PinmemberMember 1069685125-Mar-14 5:20 
QuestionHow to make SSRS report to accept parameters from C# PinmemberMember 1067755917-Mar-14 12:57 
QuestionHow to download this sample code PinmemberMember 191357527-Nov-13 18:28 
SuggestionArrayList PinprofessionalRichard Deeming8-Nov-13 9:02 
GeneralGood Article PinmemberGehan Fernando3-Nov-13 23:18 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.1411023.1 | Last Updated 29 Oct 2013
Article Copyright 2013 by Sandeep Kumar Tripathi
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid