2013-05-31 99 views
0

說明:由於未處理的異常,進程已終止。 異常信息信息:System.InvalidOperationException堆棧:在 System.Data.Linq.Table 1[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].CheckReadOnly()
at System.Data.Linq.Table
1 [[系統_ 佳能,mscorlib程序, 版本= 4.0.0.0,文化=中性 公鑰= b77a5c561934e089]] DeleteOnSubmit(。系統。 _Canon)在 ReportSender.EmailReportApp.Execute()在 System.Threading.ThreadHelper.ThreadStart_Context(System.Object的)在 System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback ,System.Object,Boolean) System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback,System.Object)at System.Threading.ThreadHelper.ThreadStart()linq查詢中未處理的異常

我正在試圖在下面的查詢刪除記錄時,上面的錯誤: 這個錯誤是什麼意思準確,我該如何解決?

private void Execute() 
{ 
    try 
    { 
     // Check for a new record 
     DataClasses1DataContext dc = new DataClasses1DataContext(); 

     foreach (var item in dc.reportsSent1s) 
     { 
      string matchedCaseNumber = item.CaseNumberKey; 

      (new MyReportRenderer()).RenderTest(matchedCaseNumber); 

      dc.reportsSent1s.DeleteOnSubmit(item); 
      dc.SubmitChanges(); 
     } 
    } 
    catch (ThreadAbortException ex) 
    { 
     _log.WriteEntry(ex.StackTrace.ToString()); 
    } 
} 

的其餘代碼如下:

public class MyReportRenderer 
{ 
    private rs2005.ReportingService2005 rs; 
    private rs2005Execution.ReportExecutionService rsExec; 

    public void RenderTest(String matchedCaseNumber) 
    { 
     string HistoryID = null; 
     string deviceInfo = null; 
     string encoding = String.Empty; 
     string mimeType = String.Empty; 
     string extension = String.Empty; 
     rs2005Execution.Warning[] warnings = null; 
     string[] streamIDs = null; 

     rs = new rs2005.ReportingService2005(); 
     rsExec = new rs2005Execution.ReportExecutionService(); 
     rs.Credentials = System.Net.CredentialCache.DefaultCredentials; 
     rsExec.Credentials = System.Net.CredentialCache.DefaultCredentials; 
     rs.Url = "http://***.**.***.**/ReportServer_DEVELOPMENT/ReportService2005.asmx"; 
     rsExec.Url = "http://***.**.***.**/ReportServer_DEVELOPMENT/ReportExecution2005.asmx"; 

     try 
     { 
      // Load the selected report. 
      rsExec.LoadReport("/LawDept/LawDeptTIC", HistoryID); 

      // Set the parameters for the report needed. 

      rs2005Execution.ParameterValue[] parameters = new rs2005Execution.ParameterValue[1]; 
      parameters[0] = new rs2005Execution.ParameterValue(); 
      parameters[0].Name = "CaseNumberKey"; 
      parameters[0].Value = matchedCaseNumber; 

      rsExec.SetExecutionParameters(parameters, "en-us"); 

      // get pdf of report 
      Byte[] results = rsExec.Render("PDF", deviceInfo, 
      out extension, out encoding, 
      out mimeType, out warnings, out streamIDs); 

      //pass paramaters for email 
      DataClasses1DataContext db = new DataClasses1DataContext(); 

      var matchedBRT = (from c in db.GetTable<vw_ProductClientInfo>() 
           where c.CaseNumberKey == matchedCaseNumber 
           select c.BRTNumber).SingleOrDefault(); 

      var matchedAdd = (from c in db.GetTable<vw_ProductClientInfo>() 
           where c.CaseNumberKey == matchedCaseNumber 
           select c.Premises).SingleOrDefault(); 

      //send email with attachment 
      MailMessage message = new MailMessage("[email protected]", "[email protected]", "Report for BRT # " + matchedAdd, "Attached if the Tax Information Certificate for the aboved captioned BRT Number"); 
      MailAddress copy = new MailAddress("[email protected]"); 
      message.CC.Add(copy); 
      SmtpClient emailClient = new SmtpClient("***.**.***.**"); 
      message.Attachments.Add(new Attachment(new MemoryStream(results), String.Format("{0}" + matchedBRT + ".pdf", "BRT"))); 
      emailClient.Send(message); 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine(ex.Message); 
     } 
    } 
} 
+0

這可能是因爲你試圖從枚舉中提交你的刪除更改?你有沒有嘗試移動循環外的提交? –

+0

@喬治約翰斯頓:這是一個很好的理論。在表上使用一個活動的枚舉器可以激活上下文中的只讀鎖,這可以解釋對'CheckReadOnly'的失敗調用。 – jason

+0

@GeorgeJohnston我也檢查了這個理論,並把刪除函數放在emailClient.Send(message)後面的最後。並仍然得到相同的錯誤。我們有沒有更好的地方去刪除?問題是這張桌子是活躍的,或者我會像對待非服務版本一樣殺死整個事物。 – korrowan

回答

1

看起來像LINQ to SQL是檢查如果表是隻讀或沒有,被挑剔的表是隻讀的,拋出的結果(不能從只讀表中刪除)。因此,您需要調查爲什麼LINQ to SQL認爲該表是隻讀的。你在桌上有主鍵嗎?我知道沒有一個是LINQ to SQL認爲表是隻讀的常見原因(LINQ to SQL需要它爲identity map)。如果這不起作用,George Johnstonsuggestion嘗試將呼叫移至SubmitChanges以外的循環是很好的;在桌面上獲得一個枚舉器可能會在上下文中激活只讀鎖,這就是爲什麼CheckReadOnly在調用提交失敗。

+0

以及它確實有一個主鍵,但是當我導入表時,我顯然沒有將它設置爲一個。我繼續前進,在數據上下文中設置爲PK,但是它出現了同樣的錯誤。 – korrowan

+0

@korrowan:嘗試George的建議。這是一個好理論;在桌上拿一個枚舉器可能會激活一個只讀鎖,這就是爲什麼'CheckReadOnly'失敗。 – jason

+0

我認爲你的模型仍然有問題。當您修改數據庫時,是否更新了模型和類?你可以在運行時使用類似'(從dc.Mapping.GetTable(typeof(reportsSent1))中的c來檢查你的模型RowType.DataMembers select new {c.DbType,c.Name,c.IsPrimaryKey});' – sgmoore