简体   繁体   中英

Windows service fails to open the connection

My C# Windows service sends emails automatically. Before sending I need to convert and attach my report as a PDF. I get an exception on the line below when I try to export the PDF:

cr.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, savePath);

Failed to open the connection (ODBC)

The same code functions properly in a windows form application. How can I resolve this?

public void GenerateReport(string rptpath, int no)
{
  try
  {
    string savePath = ConfigurationManager.AppSettings["Savepath"];
    ReportDocument cr = new ReportDocument();

    cr.Load(rptpath);

    TableLogOnInfo logInfo = new TableLogOnInfo();

    Tables crTables = cr.Database.Tables;

    foreach (Table crTable in crTables)
    {
      logInfo.ConnectionInfo.UserID = "sa";
      logInfo.ConnectionInfo.DatabaseName = "SunVue";
      logInfo.ConnectionInfo.ServerName = "SunVue";
      logInfo.ConnectionInfo.Password = "sa";
      logInfo.ConnectionInfo.IntegratedSecurity = false;
      crTable.ApplyLogOnInfo(logInfo);

      DateTime enddate = DateTime.Today;
      DateTime startdate = enddate.AddDays(-7);
      string reportFormula = string.Empty;

      if (no == 201)
      {
        cr.SetParameterValue("DateRange",startdate.ToString("dd'/'MM'/'yyyy")
                                + " to " + enddate.ToString("dd'/'MM'/'yyyy"));

        reportFormula = "{spilInvNum.OrderDate} in DateTime ("
                        + startdate.Year.ToString() + ","
                        + startdate.Month.ToString() + "," +
                        + startdate.Day.ToString() + ", 00, 00, 00) to DateTime ("
                        + enddate.Year.ToString() + ","
                        + enddate.Month.ToString() + ","
                        + enddate.Day.ToString() + ", 00, 00, 00)";

        //reportFormula = "{spilInvNum.OrderDate} in Date(" + startdate.Year.ToString() + "," + startdate.Month.ToString() + "," + startdate.Day.ToString() + ") " + " to DateTime (" + enddate.Year.ToString() + "," + enddate.Month.ToString() + "," + enddate.Day.ToString() + ") " + " and {spilInvNum.DocState} <> 6 and {spilInvNum.DocType} = 4 and {spilInvNum.AccountID} <> 2592";
        cr.RecordSelectionFormula = reportFormula;
      }
      cr.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, savePath);
      WriteToFile(savePath);
    }
  }
}

How you set up your connection in OBDC.

  1. Did you try to test the connection upon stablishing on ODBC?
  2. How did you do to use connection, in report wizard? using OBDC created? -upon connecting on CR wizard you should be promted if you have a false connection created.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM