Click here to Skip to main content
15,899,124 members
Articles / Database Development / SQL Server
Article

Generating PDF reports programmatically using SQL Server Reporting Services 2005, in C#

Rate me:
Please Sign up or sign in to vote.
2.52/5 (19 votes)
12 Sep 2006 499.4K   68   47
An article on how to generate PDF reports programmatically using SQL Server Reporting Services 2005, in C#.

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:
    C#
    private rs2005.ReportingService2005 rs;
    private rsExecService.ReportExecutionService rsExec;
  • Step 4: Initialize the web services and set the authentication for the web services.
    C#
    // Create a new proxy to the web service
    rs = new rs2005.ReportingService2005();
    rsExec = new rsExecService.ReportExecutionService();
    
    // Authenticate to the Web service using Windows credentials
    rs.Credentials = System.Net.CredentialCache.DefaultCredentials;
    rsExec.Credentials = System.Net.CredentialCache.DefaultCredentials;
    
    // Assign the URL of the Web service
    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.
    C#
    // Prepare Render arguments
    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;
    
    // Default Path;
    string fileName = @"c:\samplereport.pdf";
    
    // Define variables needed for GetParameters() method
    // Get the report name
    string _reportName = @"/MyReports/Report";
    string _historyID = null;
    bool _forRendering = false;
    ParameterValue[] _values = null;
    DataSourceCredentials[] _credentials = null;
    ReportParameter[] _parameters = null;
    
    try
    {
      // Get if any parameters needed.
      _parameters = rs.GetReportParameters(_report, _historyID, 
                    _forRendering, _values, _credentials);
    
      // Load the selected report.
      rsExecService.ExecutionInfo ei = 
            rsExec.LoadReport(_reportName, historyID);
    
      // Prepare report parameter.
      // Set the parameters for the report needed.
      rsExecService.ParameterValue[] parameters = 
             new rsExecService.ParameterValue[1];
    
      // Place to include the parameter.
      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);
    
      // Create a file stream and write the report to it
      using (FileStream stream = File.OpenWrite(fileName))
      {
        stream.Write(results, 0, results.Length);
      }
    }
    catch (Exception ex)
    {
      MessageBox.Show( ex.Message);
    }

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
France France
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalpdf blank pages problem Pin
Member 221258121-Jul-08 9:58
Member 221258121-Jul-08 9:58 
Questioni have also the problem when exporting to pdf Pin
Member 221258121-Jul-08 4:52
Member 221258121-Jul-08 4:52 
GeneralCannot Create new Subscription SSRS Report...... Pin
sriramjava5-Jun-08 3:22
sriramjava5-Jun-08 3:22 
Questionblank pages for when I exported to pdf format? Pin
kuncha4-Jun-08 17:54
kuncha4-Jun-08 17:54 
GeneralGetting a large blank spaces while exporting to PDF Pin
enihar15-Apr-08 4:34
enihar15-Apr-08 4:34 
GeneralRe: Getting a large blank spaces while exporting to PDF Pin
tobiasboehler9-Sep-08 2:39
tobiasboehler9-Sep-08 2:39 
GeneralProblems with reports that take parameters Pin
mtembene22-Aug-07 10:39
mtembene22-Aug-07 10:39 
GeneralRe: Problems with reports that take parameters Pin
mtembene22-Aug-07 11:44
mtembene22-Aug-07 11:44 
OK I figured it out after a bit of hacking.

In the end I discovered that though the parameter I had to set was a string the report writer and a set of predefined valid parameter values that had to be met. That was my first problem.

When running the report manually I could pick the parameter I wanted from a drop down list. The drop downlist actually provided a Guid as a parameter to the report.

So in the end I had to create a Guid in my code. Assign it a valid value and then add that value as a string to the parameter for my report.

Obvious in the end.


QuestionRe: Problems with reports that take parameters Pin
shayles30-Mar-08 19:08
shayles30-Mar-08 19:08 
GeneralHelp Getting Running... Pin
corpr812-Jun-07 11:20
corpr812-Jun-07 11:20 
GeneralRe: Help Getting Running... Pin
corpr813-Jun-07 2:33
corpr813-Jun-07 2:33 
GeneralRe: Help Getting Running... Pin
vikas chaudhary13-Jun-07 3:37
vikas chaudhary13-Jun-07 3:37 
GeneralRe: Help Getting Running... Pin
corpr813-Jun-07 4:35
corpr813-Jun-07 4:35 
GeneralRe: Help Getting Running... Pin
corpr813-Jun-07 13:53
corpr813-Jun-07 13:53 
GeneralRe: Help Getting Running... Pin
JaneMcAbbey21-Aug-08 10:37
JaneMcAbbey21-Aug-08 10:37 
GeneralRe: Help Getting Running... Pin
JaneMcAbbey21-Aug-08 10:49
JaneMcAbbey21-Aug-08 10:49 
GeneralREGARDING REPORTNAME Pin
mahi988514-May-07 4:33
mahi988514-May-07 4:33 
GeneralRe: REGARDING REPORTNAME Pin
vikas chaudhary31-May-07 18:53
vikas chaudhary31-May-07 18:53 
GeneralCompletely lost... Pin
jbzcooper19-Apr-07 13:33
jbzcooper19-Apr-07 13:33 
QuestionAnyone actually able to open the pdf? Pin
qtran020317-Apr-07 10:33
qtran020317-Apr-07 10:33 
AnswerRe: Anyone actually able to open the pdf? Pin
qtran020318-Apr-07 3:39
qtran020318-Apr-07 3:39 
QuestionExporting XLS Files Pin
maskeye14-Apr-07 4:29
maskeye14-Apr-07 4:29 
AnswerRe: Exporting XLS Files Pin
maskeye14-Apr-07 5:28
maskeye14-Apr-07 5:28 
GeneralBig Help! Pin
Joe Krueger11-Apr-07 18:37
Joe Krueger11-Apr-07 18:37 
GeneralDoes not seem to work always Pin
bbzz2420-Mar-07 9:23
bbzz2420-Mar-07 9:23 

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.