asp.net 读取器关闭时调用元数据的尝试无效

0lvr5msh  于 2022-11-19  发布在  .NET
关注(0)|答案(1)|浏览(115)

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

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();
}

连接已打开。我的问题是从表中检索空值时,列中有很多空值。它会给予我错误“当读取器关闭时,调用元数据的尝试无效”。我该如何解决这个问题。谢谢。
对“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)
oxcyiej7

oxcyiej71#

试试看:

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

相关问题