我將我的專案Microsoft.Practices.EnterpriseLibrary.Data.dll版本 4.0.0.0 更新為 5.0.414.0。然后我得到了這個錯誤。以前它可以正常作業。
資料源是無效型別。它必須是 IListSource、IEnumerable 或 IDataSource。 說明:執行當前 Web 請求期間發生未處理的例外。請查看堆疊跟蹤以獲取有關錯誤及其源自代碼的位置的更多資訊。
例外詳細資訊:System.InvalidOperationException:資料源是無效型別。它必須是 IListSource、IEnumerable 或 IDataSource。
this.CategoryDropDownList.DataTextField = "CAT_NAME";
this.CategoryDropDownList.DataValueField = "CAT_ID";
this.CategoryDropDownList.DataSource = mgr.GetCategories();// here error occurred
this.CategoryDropDownList.DataBind();
內部GetCategories方法 -
public IDataReader GetCategories()
{
IDataReader reader = null;
using (var cmd = MyDB.GetSqlStringCommand(MySqlScriptor.GetSql("QueryCategories")))
{
reader = MyDB.ExecuteReader(cmd);
}
return reader;
}
ExecuteReader總是回傳IDataReader物件。
我試圖轉換IDataReader為“IDataSource”。但它給了我這樣的錯誤。
無法將“Microsoft.Practices.EnterpriseLibrary.Data.RefCountingDataReader”型別的物件轉換為“System.Web.UI.IDataSource”型別。
public IDataSource GetCategories()
{
IDataSource reader = null;
using (var cmd = MyDB.GetSqlStringCommand(MySqlScriptor.GetSql("QueryCategories")))
{
reader = (IDataSource)MyDB.ExecuteReader(cmd);
}
return reader;
}
請給我一個解決方案。謝謝
uj5u.com熱心網友回復:
@jmcilhinney 的建議可能有效,尤其是如果此代碼在更新之前有效:
public IDataSource GetCategories()
{
IDataSource reader = null;
using (var cmd = MyDB.GetSqlStringCommand(MySqlScriptor.GetSql("QueryCategories")))
{
reader = (IDataSource)MyDB.ExecuteReader(cmd).InnerReader;
}
return reader;
}
但是我沒有那么舊的環境可以測驗(而且我有一些非常古老的代碼庫)。除了執行Reader,您可以使用DataSet從命令回傳 a ExecuteDataSet,DataSet然后有一個DataTable陣列,您可以從以下位置回傳第一個元素:
public IDataSource GetCategories()
{
DataTable table = null;
using (var cmd = MyDB.GetSqlStringCommand(MySqlScriptor.GetSql("QueryCategories")))
{
table = MyDB.ExecuteDataSet(cmd).Tables[0];
}
return table;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/524380.html
下一篇:控制器誤解輸入?
