2011-08-12 53 views
1

我有一個存儲過程每分鐘被調用幾百次。每遇到一次,我都會遇到一個異常情況,即查詢中不存在列。這裏是例外:間斷列不屬於表異常

System.Web.HttpUnhandledException:引發類型'System.Web.HttpUnhandledException'的異常。 ---> System.ArgumentException:列'SubjectID'不屬於表。

[ArgumentException的:柱 'SubjectID' 不屬於表] System.Data.DataRow.GetDataColumn(字符串COLUMNNAME)1775157 System.Data.DataRow.get_Item(字符串COLUMNNAME)13 System.Data .DataTableReader.get_Item(字符串名稱)+66

這裏是C#調用存儲過程:

DataTableReader dtr; 
     dtr = Util.getDepartmentsByTerm(term); 
     ddlSubject.Items.Add(new ListItem("Select A Subject...", "")); 
     while (dtr.Read()) 
     { 
      //The following line throws the exception! 
      var value = (string)dtr["SubjectID"]; 
      var text = (string)dtr["title"]; 
      var count = (int) dtr["Count"]; 
      //if (!text.Contains("(0)")) 
      if(count > 0) 
       ddlSubject.Items.Add(new ListItem(text, value)); 
     } 

很簡單......這裏的存儲過程:

ALTER PROCEDURE [dbo].[selectDepartmentsByTerm] 

( 

@term nvarchar(50), 
@version int 

) 

AS  
SELECT SubjectID, Subject as Title,(select count(distinct courseno) 
    from RegistrationBlock WITH (NOLOCK) 
where RegistrationBlock.Subject = Subjects.SubjectID 
    and RegistrationBlock.version = @version 
    and (@term = '' OR RegistrationBlock.term = @term)) as "Count" 
    FROM Subjects WITH (NOLOCK) 
where Version = @version 
order by subject 
GO 
+2

我見過一篇文章說這個問題可能是由連接池中的損壞引起的。確保您的連接全部正確關閉。您可以閱讀更多診斷/修復問題的方法:http://www.ksvali.com/2010/08/solution-to-random-error-column-does-not-belong-to-table/ –

+0

謝謝湯姆我很感激! – Robert

回答