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;
rptViewer.ServerReport.ReportServerUrl = new Uri(urlReportServer);
rptViewer.ServerReport.ReportPath = "/ReportName";
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];
}
rptViewer.ServerReport.SetParameters(param);
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.