做了一个用户管理系统的界面  却在添加新用户信息的时候出了问题    在button2的更新成功的情况下 Button3的insert却始终无法执行  也没有报错 觉得自己的逻辑判断也没有错误  却始终执行不到弹窗那一步  所以求助各位解决。
 protected void Button2_Click(object sender, EventArgs e)
    {
        Button3.Enabled = false;
        string sql = "server=localhost;database=yh;Trusted_Connection=true";
        SqlConnection sc = new SqlConnection(sql);//数据库的连接        try
        {            sc.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.Text;
            cmd.Connection = sc;            if (RadioButton1.Checked)
            {
                cmd.CommandText = "update yhxx set 姓名=('" + TextBox1.Text + "'),性别=('" + RadioButton1.Text + "') ,籍贯=('" + TextBox3.Text + "'),专业=('" + TextBox4.Text + "') where 用户名='" + this.TextBox6.Text + "'";                if (cmd.ExecuteNonQuery() > 0)
                {
                    Response.Write("<script>alert('修改成功!')</script>");                    Button1.Enabled = true;
                }
                else
                {
                    Response.Write("<script>alert('修改失败!')</script>");
                }
            }
            else
            {
                cmd.CommandText = "update yhxx set 姓名=('" + TextBox1.Text + "'),性别=('" + RadioButton2.Text + "') ,籍贯=('" + TextBox3.Text + "'),专业=('" + TextBox4.Text + "') where 用户名='" + this.TextBox6.Text + "'";
            }
        }
        catch
        {        }
        finally
        {
            
        }
    }
     protected void Button3_Click(object sender, EventArgs e)
    {        string sql = "server=localhost;database=yh;Trusted_Connection=true";
        SqlConnection sc = new SqlConnection(sql);//数据库的连接
        string yhm = this.TextBox6.Text;
        try
        {
            
            sc.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.Text;
            cmd.Connection = sc;
            cmd.CommandText = "select 用户名 from yhxx";//先查询用户信息数据库中是否有相同的用户信息
            SqlDataReader sdr = cmd.ExecuteReader();
            sdr.Read();
            while (yhm.Equals(sdr[0].ToString()))
            {
                Response.Write("<script>alert('用户信息已存在')</script>");            }            try
            {
                sc.Open();
                SqlCommand cc = new SqlCommand();
                cc.CommandType = CommandType.Text;
                cc.Connection = sc;
                if (RadioButton1.Checked)
                {
                    cc.CommandText = "insert into yhxx(用户名,姓名,性别,籍贯,专业) values('" + yhm + "','" + TextBox1.Text + "','" + RadioButton1.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "')";
                    if (cc.ExecuteNonQuery() > 0)
                    {
                        Response.Write("<script>alert('添加成功!')</script>");
                    }
                    else
                    {
                        Response.Write("<script>alert('添加失败!')</script>");
                    }
                  
                }
                else
                {
                    cc.CommandText = "insert into yhxx(用户名,姓名,性别,籍贯,专业) values('" +yhm + "','" + TextBox1.Text + "','" + RadioButton2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "')";
                    if (cc.ExecuteNonQuery() > 0)
                    {
                        Response.Write("<script>alert('添加成功!')</script>");
                    }
                    else
                    {
                        Response.Write("<script>alert('添加失败!')</script>");
                    }                }
            }
            catch
            {            }
                 }
        catch
        { 
        
        }
        finally
        {
            sc.Close();
        }    }

解决方案 »

  1.   

    //try,sdr用完后关闭试试。
    sdr.Read();
      while (yhm.Equals(sdr[0].ToString()))
      {
      Response.Write("<script>alert('用户信息已存在')</script>");  }sdr.Close();
      

  2.   

    好了  我搞定了 在
     cmd.CommandText = "select 用户名 from yhxx";//先查询用户信息数据库中是否有相同的用户信息
      SqlDataReader sdr = cmd.ExecuteReader();
      sdr.Read();
      while (yhm.Equals(sdr[0].ToString()))
      {
      Response.Write("<script>alert('用户信息已存在')</script>");  }这句代码后面就该关闭数据库了 加上sc.Close();就ok
    谢谢你啦  如果可以可以交个朋友不 616858368
      

  3.   

    下次贴代码的时候注意格式[ code=C# ]代码放这里[ /code ]