protected void Button1_Click(object sender, EventArgs e)
{
sqlcon = new SqlConnection(Constr);
sqlcon.Open();
string sql = "select * from _User where ID='" + TextBox1.Text.Trim() + "'";
SqlCommand cmd = new SqlCommand(sql, sqlcon);
cmd.CommandText = sql; string sql1 = "select * from _User where Password='" + TextBox2.Text.Trim() + "'";
SqlCommand cmd1 = new SqlCommand(sql1, sqlcon);
cmd1.CommandText = sql1; if(_ID.Text.Trim()==" ") //如果没有输入直接也可以登录,不明白为什么会这样
{
_ID.Visible = true;
_ID.Text = "请输入用户名!";
}
else if (_password.Text=="")
{
_password.Visible = true;
_password.Text = "请输入密码!";
}
else if (null == cmd.ExecuteScalar())
{
_ID.Visible = true;
_ID.Text = "用户名不存在!";
}
else if (null == cmd1.ExecuteScalar())
{
_password.Visible = true;
_password.Text = "密码不正确!";
}
else
{
Session["ID"] = TextBox1.Text;
Page.Response.Redirect("mainpage.aspx");
}
sqlcon.Close();
}
{
_ID.Visible = true;
_ID.Text = "请输入用户名!";
}
else if (_password.Text=="")
{
_password.Visible = true;
_password.Text = "请输入密码!";
}把这些放在执行sql的上边
不然sql已经执行了
你才判断,肯定要执行了
protected void Button1_Click(object sender, EventArgs e)
{
if(_ID.Text.Trim()=="") //如果没有输入直接也可以登录,不明白为什么会这样
{
_ID.Visible = true;
_ID.Text = "请输入用户名!";
return;
}
if (_password.Text=="")
{
_password.Visible = true;
_password.Text = "请输入密码!";
return;
} sqlcon = new SqlConnection(Constr);
sqlcon.Open();
string sql = "select * from _User where ID='" + TextBox1.Text.Trim() + "'";
SqlCommand cmd = new SqlCommand(sql, sqlcon);
cmd.CommandText = sql; string sql1 = "select * from _User where Password='" + TextBox2.Text.Trim() + "'";
SqlCommand cmd1 = new SqlCommand(sql1, sqlcon);
cmd1.CommandText = sql1; if (null == cmd.ExecuteScalar())
{
_ID.Visible = true;
_ID.Text = "用户名不存在!";
}
else if (null == cmd1.ExecuteScalar())
{
_password.Visible = true;
_password.Text = "密码不正确!";
}
else
{
Session["ID"] = TextBox1.Text;
Page.Response.Redirect("mainpage.aspx");
}
sqlcon.Close();
}
顶死这个,要知道,你现在已是去掉了两边空格,那么如果不输入,则文本框中的值是不为空格的(请注意空格和空字符串的区别),好好理解它!如果真的很难理解,那请你告诉我,下面两个字符串相等不""my"+" name"+" is";"my"+"name"+"is";在C#中,可以把空格也看成一个对象