0
我使用下面的代碼從表無效試圖調用元數據時,讀取器已關閉
SqlCommand cmd1 = new SqlCommand();
SqlDataReader reader1 = null;
cmd1.CommandType = CommandType.Text;
cmd1.Connection = conn;
cmd1.CommandText = "......";
reader1 = cmd1.ExecuteReader();
while (reader1.Read())
{
Label32.Text = (reader["Unit"]).ToString();
}
連接打開檢索列的任何想法。我的問題是,當從表中檢索到空值時,列中有很多空值。它會給我錯誤「無效嘗試在閱讀器關閉時調用MetaData。」我怎麼能解決這個問題。謝謝。
在「Label32.Text =(reader [」Unit「])上引發了異常ToString();」
堆棧跟蹤:
at System.Data.SqlClient.SqlDataReader.get_MetaData()
at System.Data.SqlClient.SqlDataReader.CheckMetaDataIsReady()
at System.Data.SqlClient.SqlDataReader.GetOrdinal(String name)
at System.Data.SqlClient.SqlDataReader.get_Item(String name)
at TSOD._1Test.Page_Load(Object sender, EventArgs e) in h:\TSODNEW\TSOD\1Test.aspx.cs:line 75
at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
at System.Web.UI.Control.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
難道真的該行的'while'環路的投擲例外呢?你能捕捉到哪一行引發異常幷包含堆棧跟蹤嗎? – David 2014-09-24 17:06:33
'reader'應該是'reader1 [「Unit」]' – 2014-09-24 17:06:42
@RickS,非常感謝您指出我犯的這樣一個愚蠢的錯誤! – Ivancao 2014-09-24 17:22:16