当读者关闭任何想法时无效尝试调用MetaData

时间:2014-09-24 17:02:14

标签: sql asp.net

我使用以下代码从表中检索列

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”。我怎么能解决这个问题。感谢。

Exception被抛到“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)

1 个答案:

答案 0 :(得分:0)

试试这个:

while (reader1.Read())
{
  Label32.Text = (reader1["Unit"]).ToString();
}