troubleshooting Question

How to save SSRS report to pdf from win application

Avatar of Tups001
Tups001 asked on
.NET ProgrammingMicrosoft SQL Server 2005
1 Comment1 Solution3103 ViewsLast Modified:
Hey guys,
I'm trying to save reports from reporting services to my local drive from an application rather than the report manager.
The problem I'm getting is actually creating the PDF and writing my data to that file.
I'm a SQL and SSRS developer but I'm new to C# and winapps. I've been asked to host the reports within an app, hense the query.
The below code all works great until the creation of the pdf - which should be the easiest part! :)
It creates a Pdf, writes the data - apparently - but when trying to open the file it fails.
In fact even if I create a file, and encode it, it won't create a pdf.
Any help is greatly appreciated.
public void SaveTo(DataTable TransactionDT)
            ReportService2005.ReportingService2005 rs;
            ReportExecution2005.ReportExecutionService rsExec;
            // Create a new proxy to the web service
            rs = new ReportService2005.ReportingService2005();
            rsExec = new ReportExecution2005.ReportExecutionService();
            // Authenticate to the Web service using Windows credentials
            rs.Credentials = System.Net.CredentialCache.DefaultCredentials;
            rsExec.Credentials = System.Net.CredentialCache.DefaultCredentials;
            rs.Url = "http://localhost/reportserver/reportservice2005.asmx";
            rsExec.Url = "http://localhost/reportserver/reportexecution2005.asmx";
            // Prepare Render arguments
            string historyID = null;
            string deviceInfo = null;
            string format = "PDF";
            Byte[] bytPDF;
            string encoding = String.Empty;
            string mimeType = String.Empty;
            string extension = String.Empty;
            ReportExecution2005.Warning[] warnings = null;
            string[] streamIDs = null;
            // Define variables needed for GetParameters() method
            // Get the report name
            string _reportName = "/Faxes/Transactions";
            string _historyID = null;
            bool _forRendering = false;
            ReportService2005.ParameterValue[] _values = null;
            ReportService2005.DataSourceCredentials[] _credentials = null;
            ReportService2005.ReportParameter[] _parameters = null;
            foreach (DataRow dr in TransactionDT.Rows)
                // Load the selected report.
                ReportExecution2005.ExecutionInfo ei = rsExec.LoadReport(_reportName, historyID);
                // Prepare report parameter.
                // Set the parameters for the report needed.
                ReportExecution2005.ParameterValue[] parameters = new ReportExecution2005.ParameterValue[1];
                // Place to include the parameter.
                parameters[0] = new ReportExecution2005.ParameterValue();
                parameters[0].Label = "TransactionID";
                parameters[0].Name = "TransactionID";
                parameters[0].Value = dr["ID"].ToString();
                rsExec.SetExecutionParameters(parameters, "en-us");
                //rsExec.Render(format.ToString(), deviceInfo,out extension, out encoding,out mimeType, out warnings, out streamIDs);
                bytPDF = rsExec.Render(format, deviceInfo,
                out extension, out encoding,
                out mimeType, out warnings, out streamIDs);
                string FileLocation = @"C:\fax_TransactionID_" + dr["ID"].ToString() + ".pdf";
                //Write file specified location
                StreamWriter WriteFile = new StreamWriter(FileLocation.ToString());
                //this test returns same error//
                FileStream fs = new FileStream(@"C:\test.pdf",  FileMode.Create, FileAccess.Write);
                StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.GetEncoding("gb2312"));
                sw.WriteLine("Hello World");
            bytPDF = null;
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 1 Comment.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 1 Comment.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros