System.InvalidOperationException: 已有打开的与此命令相关联的 DataReader,必须首先将它关闭。

renye0907 2009-03-15 09:10:21
源错误:


行 35: SqlDataAdapter da = new SqlDataAdapter(str, conn);
行 36: DataSet ds = new DataSet();
行 37: da.Fill(ds, "score");
行 38: DataGrid1.DataSource = ds;
行 39: DataGrid1.DataBind();


源文件: e:\kechen\tcmg.aspx.cs 行: 37

  
string strcon = "data source=www-51f7b2a9835\\sqlexpress;initial catalog=student;integrated security=SSPI";
SqlConnection conn = new SqlConnection(strcon);
conn.Open();
string sel="select * from teacher where t_no='"+TextBox2 .Text .ToString ().Trim ()+"'and t_pas ='"+TextBox1 .Text.ToString ().Trim () +"'";
SqlCommand cmd = new SqlCommand(sel, conn);
SqlDataReader read = cmd.ExecuteReader();
conn.Close();
if (read.Read())
{
LinkButton1.Visible = true;
LinkButton2.Visible = true;
string str = "SELECT score.c_no, sco.s_no, sco.score FROM score INNER JOIN sco ON score.c_no = sco.c_no";
SqlDataAdapter da = new SqlDataAdapter(str, conn);
DataSet ds = new DataSet();
da.Fill(ds, "score");
DataGrid1.DataSource = ds;
DataGrid1.DataBind();
}
else
{
Label1.Text = "帐号或密码有错误";
}

...全文
171 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
ysw830 2009-03-15
  • 打赏
  • 举报
回复
你的代码好像有多处错误,conn.Close();连接关闭了,是不能执行read.Read()操作的,应当等执行完read.Read()在关闭,或在程序最后关闭。执行完read.Read()后,记得要用read.close();关闭read对象。还有“score.c_no = sco.c_no”应该是“score.c_no = sco.s_no”
string strcon = "data source=www-51f7b2a9835\\sqlexpress;initial catalog=student;integrated security=SSPI";
SqlConnection conn = new SqlConnection(strcon);
conn.Open();
string sel="select * from teacher where t_no='"+TextBox2 .Text .ToString ().Trim ()+"'and t_pas ='"+TextBox1 .Text.ToString ().Trim () +"'";
SqlCommand cmd = new SqlCommand(sel, conn);
SqlDataReader read = cmd.ExecuteReader();

if (read.Read())
{
read.Close();
LinkButton1.Visible = true;
LinkButton2.Visible = true;
string str = "SELECT score.c_no, sco.s_no, sco.score FROM score INNER JOIN sco ON score.c_no = sco.s_no";
SqlDataAdapter da = new SqlDataAdapter(str, conn);
DataSet ds = new DataSet();
da.Fill(ds, "score");
DataGrid1.DataSource = ds;
DataGrid1.DataBind();
}
else
{
Label1.Text = "帐号或密码有错误";
}
conn.Close();
你再试试!
barry_bai 2009-03-15
  • 打赏
  • 举报
回复
最后加上
read.close();

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧