Link to home
Start Free TrialLog in
Avatar of MariaHalt
MariaHaltFlag for United States of America

asked on

Programmatically export an SSRS report to pdf file

I have an SSRS report that takes on parameter.  I need to loop through an array of the parameters, pass it to the report, generate a pdf of the report (i don't need to see view it at this time), save it, and move on to the next item in the array and repeat.  I need to do this in VS2010 ASP.NET C#.  Please help.
ASKER CERTIFIED SOLUTION
Avatar of TempDBA
TempDBA
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MariaHalt

ASKER

TempDBA, THANK YOU for your thorough and quick response.   I'm using SQL Server 2008. And, I have a stupid question.   I don't know what do with this information:

ReportServer2005:

Namespace: Microsoft.SqlServer.ReportingServices.ReportService2005
URL: http://servername/ReportServerName/ReportService2005.asmx?wsdl 
Declaration: ReportingService2005 rs = new ReportingService2005();

ReportExecution2005:

Namespace: Microsoft.SqlServer.ReportingServices.ReportExecutionService
URL: http://servername/ReportServerName/ReportExecution2005.asmx?wsdl 
Declaration: ReportExecutionService rsExec = new ReportExecutionService();

Looking forward to your response!
Try using 2008 instead of 2005 with your full server address. But that will also have some problem. Check the link if it works. If not that request for other experts attention:-
http://blog.softartisans.com/2009/11/19/adding-web-reference-to-ssrs-2008-in-visual-studio/
With some modifications, I DID IT!!!  

I opened a new Visual Studio Project Console Application.  I then added two Web Service References.  

1. Under Project --> Add Service Reference -> Advanced -> Add Web Reference -> URL:  http://<My Server Name>/<My Report Server Name>/reportservice2010.asmx and named it ReportService2010.
2. Under Project --> Add Service Reference -> Advanced -> Add Web Reference -> URL:  http://<My Server Name>/<My Report Server Name>/reportexecution2005.asmx and named it ReportExecution2005.

In my Program.cs code I have this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using GetProperteriesSample.ReportService2010;
using GetProperteriesSample.ReportExecution2005;

namespace GetProperteriesSample
{
    class Program
    {
        static void Main(string[] args)
        {
            ReportingService2010 rs = new ReportingService2010();
            rs.Credentials = System.Net.CredentialCache.DefaultCredentials;
            rs.Url = "http://<Server Name>/<Report Server Name>/reportservice2010.asmx";

            ReportExecutionService rsExec = new ReportExecutionService();
            rsExec.Credentials = System.Net.CredentialCache.DefaultCredentials;
            rsExec.Url = "http://<My Server Name>/<My Report Server Name>/reportexecution2005.asmx";

            string historyID = null;
            string reportPath = "/<My Folder Name>/<My SSRS Report Name>";
            rsExec.LoadReport(reportPath, historyID);

            GetProperteriesSample.ReportExecution2005.ParameterValue[] executionParams;
            executionParams = new GetProperteriesSample.ReportExecution2005.ParameterValue[1];
            executionParams[0] = new GetProperteriesSample.ReportExecution2005.ParameterValue();
            executionParams[0].Name = "My Parameter Name";
            executionParams[0].Value = "My Parameter Value";
            new GetProperteriesSample.ReportExecution2005.ParameterValue();

            rsExec.SetExecutionParameters(executionParams, "en-us");

            string deviceInfo = null;
            string extension;
            string encoding;
            string mimeType;
            GetProperteriesSample.ReportExecution2005.Warning[] warnings = null;
            string[] streamIDs = null;
            string format = "PDF";

            Byte[] results = rsExec.Render(format, deviceInfo, out extension, out mimeType, out encoding, out warnings, out streamIDs);
            FileStream stream = File.OpenWrite("c:\\My Destination Folder Name>\\<My PDF Report Name>.pdf");
            stream.Write(results, 0, results.Length);
            stream.Close();

        }
    }
}

The GetProperteriesSample is just the name of the MSDN Walkthrough I used to figure out how to add the Web Service.
I am going to further tweak the code so this executes for an array of parameters.

Thank you TempDBA for sending me down the right path!!!
Thank you!!!
Avatar of Rupzie D
Rupzie D

Hi there,

I keep getting an error saying:
the type or namespace 'ReportingService2005' does not exist in the namespace....

This is the only error I am getting. I have added the reportviewer assembly...

Any ideas?