title: Run a Crystal report from C# passing parameters without using a Viewer
body: <p>I am writing a "proof-of-concept" console app to test calling and retrieving Crystal Reports. I don't want to use a ReportViewer and instead what to retrieve reports via a stream. I am using the CRforVS redist libraries CrystalDecisions.CrystalReports.Engine and CrystalDecisions.Shared. My code is shown below:</p>
  
  <pre><code>static void Main(string[] args)
  {
      var x = new ReportDocument();
      x.Load("[REPORT PATH]");
      x.SetParameterValue("[PARAM NAME]", "[PARAM VALUE]");
      // set parameter values
  
      SetDbLoginForReport(x);
  
      Sections sections = x.ReportDefinition.Sections;
      foreach (Section section in sections)
      {
          ReportObjects reportObjects = section.ReportObjects;
          foreach (ReportObject reportObject in reportObjects)
          {
              if (reportObject.Kind == ReportObjectKind.SubreportObject)
              {
                  SubreportObject subreportObject = (SubreportObject)reportObject;
                  ReportDocument subReportDocument = subreportObject.OpenSubreport(subreportObject.SubreportName);
                  SetDbLoginForReport(subReportDocument);
              }
          }
      }
  
      var s = x.ExportToStream(ExportFormatType.CrystalReport);
  
  }
  
  private static void SetDbLoginForReport(ReportDocument document)
  {
      foreach (var table in document.Database.Tables)
      {
          var conn = new ConnectionInfo();
          var myTable = table as Table;
          var myTableLogonInfo = myTable.LogOnInfo;
          conn.ServerName = "";
          conn.DatabaseName = "";
          conn.UserID = "";
          conn.Password = "";
          myTableLogonInfo.ConnectionInfo = conn;
          myTable.ApplyLogOnInfo(myTableLogonInfo);
      }
  }
  </code></pre>
  
  <p>ExportToStream gives an error with a message of "Failed to Open Connection." and throws the following exception:</p>
  
  <pre><code>   at CrystalDecisions.ReportAppServer.ConvertDotNetToErom.ThrowDotNetException(Exception e)
     at CrystalDecisions.ReportSource.EromReportSourceBase.ExportToStream(ExportRequestContext reqContext)
     at CrystalDecisions.CrystalReports.Engine.FormatEngine.ExportToStream(ExportRequestContext reqContext)
     at CrystalDecisions.CrystalReports.Engine.ReportDocument.ExportToStream(ExportOptions options)
     at CrystalDecisions.CrystalReports.Engine.ReportDocument.ExportToStream(ExportFormatType formatType)
     at ConsoleApplication2.Program.Main(String[] args) in C:\Users\paul.welbourne\Documents\Visual Studio 2010\Projects\ConsoleApplication2\ConsoleApplication2\Program.cs:line 41
     at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
     at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
     at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
     at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
     at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
     at System.Threading.ThreadHelper.ThreadStart()
  </code></pre>
  
  <p>I am not setting any DataSources as they are already set-up in Crystal Report. Also, I am sure my connection strings are correct as I can connect to the data source's database with them. Any help would be appreciated?</p>