我的登录页面有问题。如果我不输入用户名或密码,它应该说“请输入用户名或密码”,而不是进入目标页面,我的意思是如果我点击提交按钮,则无需在登录字段中输入任何内容,它会受到欢迎页面实际上不应该发生。
这是我的代码,请有人告诉我我的错误在哪里:
public class Login
{
public string str = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString.ToString();
public int GetLogin(string UserName, string Password)
{
SqlConnection con = new SqlConnection(str);
SqlDataAdapter da = new SqlDataAdapter("select * from Login where UserName='"+UserName+"' and Password='"+Password+"'",con);
DataSet ds = new DataSet();
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
if ((ds.Tables[0].Rows[0].ItemArray[0].ToString() == UserName) && (ds.Tables[0].Rows[0].ItemArray[1].ToString() == Password))
{
return 1;
}
else
{
return 0;
}
}
else
{
return -1;
}
}
Login.aspx.cs:
protected void BtnLogin_Click(object sender, EventArgs e)
{
Session["UserName"] = TxtUserName.Text;
Login lg = new Login();
if ((lg.GetLogin(TxtUserName.Text, TxtPassword.Text) == 1))
{
Response.Redirect("c1.aspx");
}
else if((TxtUserName.Text=="")&&(TxtPassword.Text==""))
{
Lbl1.Text = "Please Enter the UserName and Password";
}
else
{
Lbl1.Text = "Sorry,Invalid UserName or Password";
}
}