Click here to Skip to main content
Licence 
First Posted 12 Sep 2006
Views 241,944
Bookmarked 58 times

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

By | 12 Sep 2006 | Article
An article on how to generate PDF reports programmatically using SQL Server Reporting Services 2005, in C#.
 
Part of The SQL Zone sponsored by
See Also

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.
    // 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.
    // 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

About the Author

Sivakumar Mahalingam

Web Developer

France France

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 9/10 Pinmembereka8084:40 14 Feb '12  
GeneralMy vote of 5 PinmemberMember 80063848:31 16 Sep '11  
GeneralOne or more data source credentials required to run the report have not been specified. ---> Microsoft.ReportingServices.ReportProcessing.ReportProcessingException: One or more data source credentials required to run the report have not been speci PinmemberDhruval Shah3:01 30 Jan '10  
GeneralRe: One or more data source credentials required to run the report have not been specified. ---> Microsoft.ReportingServices.ReportProcessing.ReportProcessingException: One or more data source credentials required to run the report have not been spec PinmemberIsta2:48 13 Jun '11  
GeneralC# Implmentation PinmemberMissy Casner3:51 23 Sep '09  
GeneralSample of url to reporting service Pinmemberd00_ape3:35 25 Aug '09  
GeneralTry this it will work. PinmemberM Varghese9:36 27 Jan '09  
GeneralRe: Try this it will work. PinmemberIsta5:46 30 Mar '10  
QuestionHow to export to PDF with password ? Pinmemberrajspselvaraj23:31 20 Nov '08  
AnswerRe: How to export to PDF with password ? PinmemberGufran Sheikh22:39 21 Mar '10  
GeneralPDF Performance PinmemberLautmann1:55 13 Nov '08  
GeneralTry this instead PinmemberMember 55285546:09 4 Oct '08  
GeneralRe: Try this instead PinmemberMember 55285546:18 4 Oct '08  
Generalpdf blank pages problem PinmemberMember 22125819:58 21 Jul '08  
Questioni have also the problem when exporting to pdf PinmemberMember 22125814:52 21 Jul '08  
GeneralCannot Create new Subscription SSRS Report...... Pinmembersriramjava3:22 5 Jun '08  
Questionblank pages for when I exported to pdf format? Pinmemberkuncha17:54 4 Jun '08  
GeneralGetting a large blank spaces while exporting to PDF Pinmemberenihar4:34 15 Apr '08  
I have a report which contains so many components.Some of these components are having the Visibility conditions.The Problem is that I am getting large white spaces between components while exporting the report to PDF.How can I avoid getting these spaces in PDF?
GeneralRe: Getting a large blank spaces while exporting to PDF Pinmembertobiasboehler2:39 9 Sep '08  
GeneralProblems with reports that take parameters Pinmembermtembene10:39 22 Aug '07  
GeneralRe: Problems with reports that take parameters Pinmembermtembene11:44 22 Aug '07  
QuestionRe: Problems with reports that take parameters Pinmembershayles19:08 30 Mar '08  
GeneralHelp Getting Running... Pinmembercorpr811:20 12 Jun '07  
GeneralRe: Help Getting Running... Pinmembercorpr82:33 13 Jun '07  
GeneralRe: Help Getting Running... Pinmembervikas chaudhary3:37 13 Jun '07  

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.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120529.1 | Last Updated 12 Sep 2006
Article Copyright 2006 by Sivakumar Mahalingam
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid