title: Save SSRS Report as PDF in C# programmatically
body: <p>I've read through multiple articles regarding this issue however they have all ended up with it not working, or are in vb.net. </p>
  
  <p><strong>What I currently have:</strong></p>
  
  <p>The reports are accessed via a URL which renders them as a PDF and saves them in the downloads folder when the user clicks on a button, these are given generic names such as OrderReport, OrderReport(1)... and so on.</p>
  
  <pre><code>var orderNum = 1;
  
  "http://Server/ReportServer_Name/Pages/ReportViewer.aspx?%2fOrderReport&amp;rs:Command=Render&amp;OrderID=" + orderNum + "&amp;rs:ClearSession=true&amp;rs:Format=PDF"
  </code></pre>
  
  <p><strong>What I am trying to acheive:</strong></p>
  
  <ul>
  <li>I would like to use C# to fetch this report if possible, and then specify a name for the PDF file and save it in the correct location.  </li>
  </ul>
  
  <p>so for example I would like to save this report in a temporary folder for now "<strong>C:\temp</strong>" with the name <strong>OrderID-1</strong>. I am using C#</p>
  
  <p>I have added in a ServiceReference into the Project i am using called ReportTestings so the reference is </p>
  
  <pre><code>using ReportTestings;
  </code></pre>
  
  <p>and the Web Reference URL:</p>
  
  <p><a href="http://Server/ReportServer_Name/ReportExecution2005.asmx" rel="noreferrer">http://Server/ReportServer_Name/ReportExecution2005.asmx</a></p>
  
  <p>(removed the actual names for security reasons) </p>
  
  <p>so based on all of this information above could someone point me in the right direction or give an example part of code, Thankyou for all that read this post or help</p>
  
  <p>using this code i get this error :(+        e   </p>
  
  <pre><code>{"Access to the path 'C:\\Program Files (x86)\\IIS Express\\report1one.pdf' is denied."}    System.Exception {System.UnauthorizedAccessException})
  </code></pre>
  
  <p>code:</p>
  
  <pre><code>    ReportExecutionService rs = new ReportExecutionService();
      rs.Credentials = new NetworkCredential("username", "password", "domain");
      rs.Url = "http://Server/ReportServer_Name/reportexecution2005.asmx";
  
      // Render arguments
      byte[] result = null;
      string reportPath = "/Invoice";
      string format = "PDF";
      string historyID = null;
      string devInfo = @"&lt;DeviceInfo&gt;&lt;Toolbar&gt;False&lt;/Toolbar&gt;&lt;/DeviceInfo&gt;";
  
      // Prepare report parameter.
      ParameterValue[] parameters = new ParameterValue[3];
      parameters[0] = new ParameterValue();
      parameters[0].Name = "InvoiceID";
      parameters[0].Value = "2";
  
      DataSourceCredentials[] credentials = null;
      string showHideToggle = null;
      string encoding;
      string mimeType;
      string extension;
      Warning[] warnings = null;
      ParameterValue[] reportHistoryParameters = null;
      string[] streamIDs = null;
  
      ExecutionInfo execInfo = new ExecutionInfo();
      ExecutionHeader execHeader = new ExecutionHeader();
  
      rs.ExecutionHeaderValue = execHeader;
  
      execInfo = rs.LoadReport(reportPath, historyID);
  
      rs.SetExecutionParameters(parameters, "en-us");
      String SessionId = rs.ExecutionHeaderValue.ExecutionID;
  
      Console.WriteLine("SessionID: {0}", rs.ExecutionHeaderValue.ExecutionID);
  
  
      try
      {
          result = rs.Render(format, devInfo, out extension, out encoding, out mimeType, out warnings, out streamIDs);
  
          execInfo = rs.GetExecutionInfo();
  
          Console.WriteLine("Execution date and time: {0}", execInfo.ExecutionDateTime);
  
  
      }
      catch (SoapException e)
      {
          Console.WriteLine(e.Detail.OuterXml);
      }
      // Write the contents of the report to an MHTML file.
      try
      {
          FileStream stream = File.Create("report1one.pdf", result.Length);
          Console.WriteLine("File created.");
          stream.Write(result, 0, result.Length);
          Console.WriteLine("Result written to the file.");
          stream.Close();
      }
      catch (Exception e)
      {
          Console.WriteLine(e.Message);
      }
  </code></pre>