Introduction
SQL Server Reporting Services (SSRS) 2005 is the latest version of the reporting technology from Microsoft. This article explains a way to create PDF reports programmatically using web services exposed by SQL Server Reporting Services 2005 in C#.
Using the code
First create a report which accepts boolean as its input parameter, and deploy that to the reporting server. The code below explains how to render the report in PDF format programmatically, using C#.
- Step 1: Create and deploy the report.
- Step 2: Add a web reference to the web services exposed by Reporting Services 2005, i.e., ReportExecution2005 and ReportService2005.
- Step 3: Declare the following variables:
private rs2005.ReportingService2005 rs;
private rsExecService.ReportExecutionService rsExec;
- Step 4: Initialize the web services and set the authentication for the web services.
rs = new rs2005.ReportingService2005();
rsExec = new rsExecService.ReportExecutionService();
rs.Credentials = System.Net.CredentialCache.DefaultCredentials;
rsExec.Credentials = System.Net.CredentialCache.DefaultCredentials;
rs.Url = "http://ICCW1023/ReportServer" +
"$REPORTSERVER2005/ReportService2005.asmx";
rsExec.Url = "http://ICCW1023/ReportServer" +
"$REPORTSERVER2005/ReportExecution2005.asmx";
- Step 5: Write code to render the report in PDF format.
string historyID = null;
string deviceInfo = null;
string format = "PDF";
Byte[] results;
string encoding = String.Empty;
string mimeType = String.Empty;
string extension = String.Empty;
rsExecService.Warning[] warnings = null;
string[] streamIDs = null;
string fileName = @"c:\samplereport.pdf";
string _reportName = @"/MyReports/Report";
string _historyID = null;
bool _forRendering = false;
ParameterValue[] _values = null;
DataSourceCredentials[] _credentials = null;
ReportParameter[] _parameters = null;
try
{
_parameters = rs.GetReportParameters(_report, _historyID,
_forRendering, _values, _credentials);
rsExecService.ExecutionInfo ei =
rsExec.LoadReport(_reportName, historyID);
rsExecService.ParameterValue[] parameters =
new rsExecService.ParameterValue[1];
if (_parameters.Length > 0 )
{
parameters[0] = new rsExecService.ParameterValue();
parameters[0].Label = "verzamelgroepAP";
parameters[0].Name = "verzamelgroepAP";
parameters[0].Value = "true";
}
rsExec.SetExecutionParameters(parameters, "en-us");
results = rsExec.Render(format, deviceInfo,
out extension, out encoding,
out mimeType, out warnings, out streamIDs);
using (FileStream stream = File.OpenWrite(fileName))
{
stream.Write(results, 0, results.Length);
}
}
catch (Exception ex)
{
MessageBox.Show( ex.Message);
}