为什么加上这个循环这后,结果就运行不出来了,一直在运行,就好像是死循环
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);
}
}
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);
}
}
{
if (TextBox2.Text != "")
try
{
这里对么?
这句是干嘛呢
设个断点调式下 看看isok的值是不是一直都没变
2.如果数据为空时,会进入死循环.....
//异常退出死循环:
catch { isOk=true; }
.....
{
}
改成
if (sr.Read())
{
}试试
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的值便可
if (!isOk)
{
Response.Write(isOk);
Label1.Text = "输入有误!";
TextBox1.Text = "";
TextBox2.Text = "";
TextBox1.Focus();
}
if里面的语句不能显示运行结果是为什么啊?
我详细解释一下我的这段代码。这段代码的时用来修改登录密码的,用户首先要先输入原来的用户名和密码,系统获得用户输入的密码,然后查看数据库中是否存在该用户名和密码,如果没有,则提示“输入有误”则该用户继续输入,直到输入正确的用户名和密码。
值没有?
用户名:
密码:
输入新密码:
输入新密码:
我是想在输入用户名和密码是可能会出错,所以想在“密码”后面加一个label,提示输入有误,然后重新输入,所以使用循环。