Avatar of Tups001
Tups001 asked on

How to save SSRS report to pdf from win application

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());
                WriteFile.Write(bytPDF);
                WriteFile.Close();
 
 
                //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");
 
                sw.Close();
 
 
 
 
           
            
            }
            
            bytPDF = null;
 
        }

Open in new window

.NET ProgrammingMicrosoft SQL Server 2005

Avatar of undefined
Last Comment
Tups001

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Tups001

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck