日期:2014-05-17  浏览次数:20416 次

阅读器关闭时尝试调用 MetaData 无效。
代码如下:
 protected void TextBox2_TextChanged(object sender, EventArgs e)
  {  
  foreach (GridViewRow gvr in this.GridView1.Rows)
  {
  TextBox num = (TextBox)gvr.FindControl("TextBox2");
  int count = Int32.Parse(num.Text);
  string bookname = gvr.Cells[0].Text;
  SqlConnection con = db.GetConnection();
  con.Open();
  SqlCommand cmd =new SqlCommand ("select stock from tb_BookInfo where BookName='"+bookname+"'",con);
  SqlDataReader sdr = cmd.ExecuteReader ();
  sdr.Read();
  int re = Convert.ToInt32(sdr[0].ToString());
  con.Close();
  if (re < count)
  {
  Response.Write(comm .MessageBox ("库存不足!","ShopCar.aspx"));
  Response.End();
  }
  }

  }

------解决方案--------------------
把con.Close();
移到foreach外面。
都关闭了还怎么读数据啊。
------解决方案--------------------
protected void TextBox2_TextChanged(object sender, EventArgs e)
{
foreach (GridViewRow gvr in this.GridView1.Rows)
{
TextBox num = (TextBox)gvr.FindControl("TextBox2");
int count = Int32.Parse(num.Text);
string bookname = gvr.Cells[0].Text;
SqlConnection con = db.GetConnection();
con.Open();
SqlCommand cmd =new SqlCommand ("select stock from tb_BookInfo where BookName='"+bookname+"'",con);
SqlDataReader sdr = cmd.ExecuteReader ();
sdr.Read();
int re = Convert.ToInt32(sdr[0].ToString());
if (re < count)
{
Response.Write(comm .MessageBox ("库存不足!","ShopCar.aspx"));
Response.End();
}
}
con.Close();
}