为什么加上这个循环这后,结果就运行不出来了,一直在运行,就好像是死循环 
protected void TextBox2_TextChanged(object sender, EventArgs e)
    {        bool isOk=false;        do
        {
            if (TextBox2.Text != "")
         
                try
                {
                    SqlConnection sqlCon = new SqlConnection();
                    sqlCon.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["powerConnectionString5"].ToString();//从Web.config文件中读取连接字符串                    sqlCon.Open();
                    string s1 = TextBox1.Text;
                    string s2 = TextBox2.Text;                    string sql = "select username from adminLogin  where username='" + s1 + "' and password='" + s2 + "'";
                    SqlCommand comn = new SqlCommand(sql, sqlCon);
                    SqlDataReader sr = comn.ExecuteReader();
                    if (sr.HasRows == true)
                    {
                        isOk = true;
                    }
                    else
                    {
                        isOk = false;
                    }
                    sr.Close();
                }
                catch { }                if (!isOk)
                {
                    Response.Write(isOk);
                    Label1.Text = "输入有误!";
                    TextBox1.Text = "";
                    TextBox2.Text = "";
                }            } while (isOk != true);
                }
    
}

解决方案 »

  1.   

    do
      {
      if (TextBox2.Text != "")
       
      try
      {
    这里对么?
      

  2.   

    while (isOk != true);
    这句是干嘛呢
    设个断点调式下 看看isok的值是不是一直都没变
      

  3.   

    楼主在textchanged里写循环想干啥?不太好吧
      

  4.   

    1.如果要是代码抛异常了,会进入死循环
    2.如果数据为空时,会进入死循环.....
    //异常退出死循环:
    catch { isOk=true; }
    .....
      

  5.   

    if (sr.HasRows == true)
      {
      }
    改成
    if (sr.Read())
      {
     }试试
      

  6.   


    protected void TextBox2_TextChanged(object sender, EventArgs e)
      {  bool isOk=false;  do
      {
      try
      {
      if (TextBox2.Text != "")  SqlConnection sqlCon = new SqlConnection();
      sqlCon.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["powerConnectionString5"].ToString();//从Web.config文件中读取连接字符串  sqlCon.Open();
      string s1 = TextBox1.Text;
      string s2 = TextBox2.Text;  string sql = "select username from adminLogin where username='" + s1 + "' and password='" + s2 + "'";
      SqlCommand comn = new SqlCommand(sql, sqlCon);
      SqlDataReader sr = comn.ExecuteReader();
      if (sr.Read())
      {
      isOk = true;
      }
      else
      {
      isOk = false;
      }
      sr.Close();
      }
      catch { }  if (!isOk)
      {
      Response.Write(isOk);
      Label1.Text = "输入有误!";
      TextBox1.Text = "";
      TextBox2.Text = "";
      }  } while (isOk == true);
          }
        
    }我还是建议LZ单步调试 只需要注意isOk的值便可
      

  7.   

    我加上断点试了,可以运行到这地方,isOk值是false,但是
     if (!isOk)
                    {
                        Response.Write(isOk);
                        Label1.Text = "输入有误!";
                        TextBox1.Text = "";
                        TextBox2.Text = "";
                        TextBox1.Focus();
                    }
    if里面的语句不能显示运行结果是为什么啊?
    我详细解释一下我的这段代码。这段代码的时用来修改登录密码的,用户首先要先输入原来的用户名和密码,系统获得用户输入的密码,然后查看数据库中是否存在该用户名和密码,如果没有,则提示“输入有误”则该用户继续输入,直到输入正确的用户名和密码。
      

  8.   

    用不着循环啊,在textbox失去焦点的时候再去查数据库,或者点击按钮的时候再查数据库就行了啊
      

  9.   

    Label1.Text = "输入有误!";
    值没有?
      

  10.   

    单步调试,查看isok的值是不是真确
      

  11.   

    对啊,不显示值,很奇怪,
    用户名:
    密码:
    输入新密码:
    输入新密码:
    我是想在输入用户名和密码是可能会出错,所以想在“密码”后面加一个label,提示输入有误,然后重新输入,所以使用循环。
      

  12.   

    不会吧,这么奇怪,isok=false  if(!isok)应该进去了啊
      

  13.   

    F11跟一下看看,是不是在前面什么地方isok给赋值了