已有打开的与此命令相关联的 DataReader,必须首先将它关闭——什么原因啊???
提示错误如下:
“/”应用程序中的服务器错误。
--------------------------------------------------------------------------------已有打开的与此命令相关联的 DataReader,必须首先将它关闭。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.InvalidOperationException: 已有打开的与此命令相关联的 DataReader,必须首先将它关闭。源错误: 
行 40:             string strSQL1 = "INSERT INTO wyx_user (wyx_name,wyx_pass) VALUES ('" + TextBox1.Text + "','" + password + "')";
行 41:             SqlCommand cmd1 = new SqlCommand(strSQL1, ConnSql);
行 42:             cmd1.ExecuteNonQuery();
行 43: 
行 44:             Response.Write("<script>alert('添加成功!')</script>");
 源文件: f:\wyx3.0\wyxback\admin_adminadd.aspx.cs    行: 42 
源码:   protected void Button1_Click(object sender, EventArgs e)
    {
        string strConn = ConfigurationManager.ConnectionStrings["SperConnectionString1"].ToString();
        SqlConnection ConnSql = new SqlConnection(strConn);
        ConnSql.Open();
        string strSQL = "SELECT * FROM wyx_user WHERE wyx_name='" + TextBox1.Text + "'";
        SqlCommand cmd = new SqlCommand(strSQL, ConnSql);
        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.Read())//如果用户名存在
        {
            Response.Write("<script>alert('对不起,用户名已被注册,请换个名称,谢谢!')</script>");        }
        else
        {
            string password = FormsAuthentication.HashPasswordForStoringInConfigFile(TextBox2.Text, "MD5");            string strSQL1 = "INSERT INTO wyx_user (wyx_name,wyx_pass) VALUES ('" + TextBox1.Text + "','" + password + "')";
            SqlCommand cmd1 = new SqlCommand(strSQL1, ConnSql);
            cmd1.ExecuteNonQuery();            Response.Write("<script>alert('添加成功!')</script>");
        }        dr.Close();
        ConnSql.Close();
        Response.Redirect("admin_admin.aspx");//返回管理页面    }
说明一下,这个程序的功能是添加管理员数据库里的管理员表是wyx_user我估计是if语句有问题,因为要考虑到如果添加的用户名已经存在,就要给出提示信息

解决方案 »

  1.   

    你把SqlDataReader的关闭写在if (dr.Read())里面第一句
      

  2.   

    看着没有问题呀.要不你换个测试的方法吧.把那条SQL语名改成
    string strSQL = "SELECT * FROM wyx_user WHERE wyx_name='" + TextBox1.Text + "'";
    改为:string strSQL = "SELECT COUNT(*) FROM wyx_user WHERE wyx_name='" + TextBox1.Text + "'";然后设一个int 变量,int count=cmd.ExecuteNonQuery();
     
    把if语句的条件改成if(count>0).就是说如果用户名存在的话.这样就行了.
      

  3.   

    cmd.ExecuteNonQuery(CommandBehavion.Close); 
    大概就是这样!
      

  4.   


    你是说这样写:
            if (dr.Read())//如果用户名存在
            {
                dr.Close();
                Response.Write("<script>alert('对不起,用户名已被注册,请换个名称,谢谢!')</script>");        }
      

  5.   

    你是说这样写:         if (dr.Read())//如果用户名存在
            {
                dr.Close();
                Response.Write("<script>alert('对不起,用户名已被注册,请换个名称,谢谢!')</script>");        }
      

  6.   

    改成这个样子即可:
    //...前面都一样
            SqlDataReader dr = cmd.ExecuteReader();
            boolean bExists;
            bExists = dr.Read();//如果用户名存在
            dr.close();
            if (bExists)
    {...}
    //后面都一样。
      

  7.   

           if (dr.Read())//如果用户名存在
            {
                Response.Write("<script>alert('对不起,用户名已被注册,请换个名称,谢谢!')</script>");        }
            else
            {
                string password = FormsAuthentication.HashPasswordForStoringInConfigFile(TextBox2.Text, "MD5");            string strSQL1 = "INSERT INTO wyx_user (wyx_name,wyx_pass) VALUES ('" + TextBox1.Text + "','" + password + "')";
                SqlCommand cmd1 = new SqlCommand(strSQL1, ConnSql);
                cmd1.ExecuteNonQuery();            Response.Write("<script>alert('添加成功!')</script>");
            }        dr.Close();
    这样写
      

  8.   

    if (dr.Read())//如果用户名存在
            {
                Response.Write("<script>alert('对不起,用户名已被注册,请换个名称,谢谢!')</script>");        }
            else
            {
                dr.Close();
                string password = FormsAuthentication.HashPasswordForStoringInConfigFile(TextBox2.Text, "MD5");            string strSQL1 = "INSERT INTO wyx_user (wyx_name,wyx_pass) VALUES ('" + TextBox1.Text + "','" + password + "')";
                SqlCommand cmd1 = new SqlCommand(strSQL1, ConnSql);
                cmd1.ExecuteNonQuery();            Response.Write("<script>alert('添加成功!')</script>");
            }        
    这样写,写到else里边的第一句!
      

  9.   

    恩 9#对的 if和else里面都该关闭  
    反正你用了sqldatareader了就在用过后第一时间关闭
      

  10.   

    答案上面已经说了,和你说一下原因,SqlDataReader对数据库的连接是独占方式,使用完后必须关闭才能执行其它的,你可以把SqlDataReader换成DataSet来处理,其优点是数据存在内存中,缺点是比SqlDataReader要慢
      

  11.   


    为何不用SqlCommand.ExecuteScalar 方法呢if(cmd.ExecuteScalar() == null) // 没有存在的用户
    {
    //插入新用户
    }
    else
    {
     //提示已存在
    }// 返回
      

  12.   

    你第二次用ExecuteReader()出错,如果第一个ExecuteReader()还不能关的话,就用另一个connection建立你用的第二个ExecuteReader(),这样就不冲突了