这根本不起作用。我已经做过很多次了,但是我不知道出了什么问题。该文本框始终显示“未找到”,而应显示用户名。 注意:文本框只是一个例子。
登录页面:
protected void login_Click(object sender, EventArgs e)
{
Session.RemoveAll();
Session.Abandon();
Session.Clear();
string username = email.Text.ToLower().Trim();
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["myConnectionString"].ToString());
con.Open();
string sql = "SELECT USERNAME, PASSWORD FROM MANAGER WHERE USERNAME = @USERNAME AND PASSWORD=@PASSWORD";
SqlCommand command = new SqlCommand(sql, con);
command.Parameters.AddWithValue("@USERNAME", username);
command.Parameters.AddWithValue("@PASSWORD", password.Text.Trim());
SqlDataAdapter da = new SqlDataAdapter(command);
DataTable dt = new DataTable();
da.Fill(dt);
command.ExecuteNonQuery();
command.Dispose();
con.Close();
if (dt.Rows.Count > 0)
{
Session["manager"] = username;
Response.Redirect("ManagerHomePage.aspx");
Session.RemoveAll();
}
else
{
Label1.Text = "Invalid Email or Password!";
}
}
ManagerHomePage.aspx: 受保护的void Page_Load(对象发送者,EventArgs e) {
if(Session["manager"]!=null)
{
TextBox1.Text = Session["manager"].ToString();
}
else
{
TextBox1.Text = "not found";
}
}
答案 0 :(得分:0)
请勿将ExecuteNonQuery
用于Select
命令。 ExecuteNonQuery
用于Insert
,Update
,Delete
命令。尝试使用ExecuteReader
贝娄是代码
protected void login_Click(object sender, EventArgs e)
{
Session.RemoveAll();
Session.Abandon();
Session.Clear();
string username = email.Text.ToLower().Trim();
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["myConnectionString"].ToString());
con.Open();
string sql = "SELECT USERNAME, PASSWORD FROM MANAGER WHERE USERNAME = @USERNAME AND PASSWORD=@PASSWORD";
SqlCommand command = new SqlCommand(sql, con);
command.Parameters.AddWithValue("@USERNAME", username);
command.Parameters.AddWithValue("@PASSWORD", password.Text.Trim());
SqlDataReader rdr=command.ExecuteReader();
if(rdr.HasRows())
{
Session["manager"] = username;
Response.Redirect("ManagerHomePage.aspx");
}
else
{
Label1.Text = "Invalid Email or Password!";
}
}