2016-09-09 95 views
0

我的C#Windows服務自動發送電子郵件。在發送之前,我需要將我的報告轉換並附加爲PDF。我得到下面當我嘗試了PDF導出就行了異常:Windows服務無法打開連接

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

在一個窗口失敗相同的代碼運行正常打開連接(ODBC)

形成應用。我該如何解決這個問題?

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); 
    } 
    } 
} 
+1

檢查文件'savePath'權限。 –

回答

0

如何在OBDC中設置連接。

  1. 您是否嘗試在建立ODBC時測試連接?
  2. 在報告嚮導中,您是如何使用連接的?使用OBDC創建? -upon在CR嚮導上連接時,如果創建了錯誤連接,則應該提示您。
+0

請查看我上面的代碼 –

+0

先查看您的odbc連接 –

相關問題