我已在Visual Studio中创建了具有SQL数据库连接的在线购物网站,并使用链接按钮将登录页面和注册页面连接到了索引页面。如果用户已登录,请显示用户名并显示注销按钮。如果用户未登录,请显示注册按钮。
但是在我用密码创建了1个用户然后进入登录页面后,它起作用了,但是在用密码创建了第二个用户之后,登录页面始终显示错误的密码,而信息却是正确的,因为我已经在SQL数据库中签入了第二个用户已注册,但仍未登录登录页面。 这些是我的登录和注册代码:
这是我的登录代码
protected void btnlogin_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connect"].ToString());
con.Open();
string query = "select * from TableUser where Username=@username AND Password=@password ";
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@username", txtusername.Text.Trim());
cmd.Parameters.AddWithValue("@password", txtpassword.Text.Trim());
int count = Convert.ToInt32(cmd.ExecuteScalar());
if (count == 1)
{
Session["username"] = txtusername.Text;
Response.Redirect("index.aspx");
}
else
{
Response.Write("Your Password and Username are Incorrect !!");
}
}
这是我的注册代码:
protected void btnsignup_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connect"].ToString());
con.Open();
string query = "insert into TableUser ([Username], [Password],[Email],[Address],[Phone]) values(@username,@password,@email,@address,@phone)";
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@username", txtusername.Text.Trim());
cmd.Parameters.AddWithValue("@password", txtpassword.Text.Trim());
cmd.Parameters.AddWithValue("@email", txtemail.Text);
cmd.Parameters.AddWithValue("@address", txtaddress.Text);
cmd.Parameters.AddWithValue("@phone", txtphone.Text);
cmd.ExecuteNonQuery();
txtusername.Text = "";
txtpassword.Text = "";
txtemail.Text = "";
txtaddress.Text = "";
txtphone.Text = "";
Session["username"] = txtusername.Text;
Response.Redirect("Login.aspx");
}
这是我的母版页代码:
protected void Page_Load(object sender, EventArgs e)
{
if (Session["username"] != null)
{
Label1.Text = "Welcome" + Session["username"].ToString();
LinkButton1.Visible = true;
LinkButton2.Visible = false;
}
else
{
LinkButton1.Visible = false;
LinkButton2.Visible = true;
}
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
Session.Abandon();
Session.Clear();
Response.Redirect("Login.aspx");
}
protected void LinkButton2_Click(object sender, EventArgs e)
{
Response.Redirect("Login.aspx");
}
请帮助我解决这个问题。