Click here to Skip to main content
15,891,864 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 499K   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

 
QuestionAdobe product requirement to generate pdf reports using SSRS Pin
ajknu30-Oct-17 17:45
ajknu30-Oct-17 17:45 
QuestionThe vb.net Equivalent Version Pin
Alex Lush17-Dec-15 23:12
Alex Lush17-Dec-15 23:12 
This is the vb.net equivalent code.(Slightly changed/simplified the Parameters section as I always know what params are needed for my reports).

VB
Imports System
Imports System.IO
Imports System.Web.Services.Protocols
Imports <yourproject>.rs2005

Module Module1
        Sub main()

        'Create a new proxy to the web service
        Dim rs As New rs2005.ReportingService2005()
        Dim rsExec As 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://server/site/reportservice2005.asmx"
        rsExec.Url = "http://server/site/ReportExecution2005.asmx"

        'Prepare Render arguments
        Dim historyID As String = Nothing
        Dim deviceInfo As String = Nothing
        Dim format As String = "PDF"
        Dim results As [Byte]()
        Dim encoding As String = [String].Empty
        Dim mimeType As String = [String].Empty
        Dim extension As String = [String].Empty
        Dim warnings As rsExecService.Warning() = Nothing
        Dim streamIDs As String() = Nothing

        'Default Path;
        Dim fileName As String = "C:\Users\user\Desktop\samplereport.pdf"

        'Define variables needed for GetParameters() method
        Dim _reportName As String = "/site/reportname"
        Dim _historyID As String = Nothing
        Dim _forRendering As Boolean = False
        Dim _values As ParameterValue() = Nothing
        Dim _credentials As DataSourceCredentials() = Nothing
        Dim paramcount As Integer = 0

        Try

            'Load the selected report.
            Dim ei As rsExecService.ExecutionInfo = rsExec.LoadReport(_reportName, historyID)

            'Define the parameters for the report
            Dim parameters(2) As rsExecService.ParameterValue

            parameters(0) = New rsExecService.ParameterValue()
            parameters(0).Name = "warehouse"
            parameters(0).Value = "01"

            parameters(1) = New rsExecService.ParameterValue()
            parameters(1).Name = "product"
            parameters(1).Value = "222153-003"

            parameters(2) = New rsExecService.ParameterValue()
            parameters(2).Name = "BOMQty"
            parameters(2).Value = "99"

            'Set the parameters for the report
            rsExec.SetExecutionParameters(parameters, "en-us")

            'Render the report
            results = rsExec.Render(format, deviceInfo, extension, encoding, mimeType, warnings, streamIDs)

            'Create a file stream and write the report to it
            Using stream As FileStream = File.OpenWrite(fileName)
                stream.Write(results, 0, results.Length)
            End Using
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
End Module

Hopefully someone may find it of use.

Regards,

Alex
QuestionWell done sir! Pin
Alex Lush17-Dec-15 7:12
Alex Lush17-Dec-15 7:12 
GeneralI solved big problem which i faced using this article. Pin
Member 34355528-Jan-15 1:39
Member 34355528-Jan-15 1:39 
QuestionGenerating PDF reports programmatically using SQL Server Reporting Services 2005, in C# Pin
Aniket Banerjee17-Dec-13 0:04
Aniket Banerjee17-Dec-13 0:04 
Questioncreate file on local machine Pin
isthmuskn15-Dec-13 13:03
isthmuskn15-Dec-13 13:03 
Questionabout adding web reference Pin
Harini 98555548-Oct-13 23:11
Harini 98555548-Oct-13 23:11 
GeneralMy vote of 9/10 Pin
eka80814-Feb-12 4:40
eka80814-Feb-12 4:40 
GeneralMy vote of 5 Pin
Member 800638416-Sep-11 8:31
Member 800638416-Sep-11 8:31 
GeneralOne or more data source credentials required to run the report have not been specified. ---&gt; Microsoft.ReportingServices.ReportProcessing.ReportProcessingException: One or more data source credentials required to run the report have not been speci Pin
Dhruval Shah30-Jan-10 3:01
Dhruval Shah30-Jan-10 3:01 
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 Pin
Ista13-Jun-11 2:48
Ista13-Jun-11 2:48 
GeneralC# Implmentation Pin
Missy Casner23-Sep-09 3:51
Missy Casner23-Sep-09 3:51 
GeneralSample of url to reporting service Pin
anderslundsgard25-Aug-09 3:35
anderslundsgard25-Aug-09 3:35 
GeneralTry this it will work. Pin
M Varghese27-Jan-09 9:36
M Varghese27-Jan-09 9:36 
GeneralRe: Try this it will work. Pin
Ista30-Mar-10 5:46
Ista30-Mar-10 5:46 
QuestionHow to export to PDF with password ? Pin
rajspselvaraj20-Nov-08 23:31
rajspselvaraj20-Nov-08 23:31 
AnswerRe: How to export to PDF with password ? Pin
Gufran Sheikh21-Mar-10 22:39
Gufran Sheikh21-Mar-10 22:39 
GeneralPDF Performance Pin
Lautmann13-Nov-08 1:55
Lautmann13-Nov-08 1:55 
GeneralTry this instead Pin
Member 55285544-Oct-08 6:09
Member 55285544-Oct-08 6:09 
GeneralRe: Try this instead Pin
Member 55285544-Oct-08 6:18
Member 55285544-Oct-08 6:18 
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 

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.