我有四个学生数据库,教师,非教师和管理员,在登录页面上我有一个用于输入的用户 ID 和密码文本框以及用户类型下拉列表......我在登录页面上添加他们的姓名会话显然,当我在我的输出控制台和母版页上 debug.write 它时,它显示了所有四个用户的名称,因此我猜会话也是在那里创建的......我的母版页代码如下:
if (Session["StudFirstName"] != null)
{
Label1.Text = Session["StudId"].ToString();
}
if (Session["FacultyFirstName"] != null)
{
Label1.Text = Session["StudId"].ToString();
}
if (Session["AccEmployeeName"] != null)
{
Label1.Text = Session["StudId"].ToString();
}
if (Session["AdminName"] != null)
{
Label1.Text = Session["StudId"].ToString();
}
else
{
Response.Redirect("Login.aspx");
}
但是当我在浏览器上运行它并在登录页面上点击提交时,正如我所说的会话已创建但页面永远不会进入母版页,它只是刷新并自行登录,我也试图清除 nullexceptions...怎么能我解决了这个异常??并问我是否需要更多信息.... :)
这是我的提交按钮代码:
if (DropDownList1.SelectedItem.Text == "Student")
{
SqlConnection con = new SqlConnection(strcon);
SqlCommand cmd = new SqlCommand("Select StudFirstName from Student where StudId=@sid and Password=@pw", con);
cmd.Parameters.AddWithValue("@sid", TextBox1.Text);
cmd.Parameters.AddWithValue("@pw", TextBox2.Text);
con.Open();
string name = Convert.ToString(cmd.ExecuteScalar());
con.Close();
if (String.IsNullOrEmpty(name))
Label1.Text = "Sorry! Invalid User ID or Password!";
else
{
if (CheckBox1.Checked)
{
Response.Cookies["UName"].Value = TextBox1.Text;
Response.Cookies["PWD"].Value = TextBox2.Text;
Response.Cookies["UName"].Expires = DateTime.Now.AddMonths(2);
Response.Cookies["PWD"].Expires = DateTime.Now.AddMonths(2);
}
Session.Add("studid", TextBox1.Text);
Session.Add("studfirstname", name);
FormsAuthentication.RedirectFromLoginPage(name, false);
Debug.Write(Session["studfirstname"].ToString());
}
}
if (DropDownList1.SelectedItem.Text == "Faculty Staff")
{
SqlConnection con = new SqlConnection(strcon);
SqlCommand cmd = new SqlCommand("Select FacultyFirstName from Faculty where FacultyId=@fid and Password=@pw", con);
cmd.Parameters.AddWithValue("@fid", TextBox1.Text);
cmd.Parameters.AddWithValue("@pw", TextBox2.Text);
con.Open();
string name = Convert.ToString(cmd.ExecuteScalar());
con.Close();
if (String.IsNullOrEmpty(name))
Label1.Text = "Sorry! Invalid User ID or Password!";
else
{
if (CheckBox1.Checked)
{
Response.Cookies["UName"].Value = TextBox1.Text;
Response.Cookies["PWD"].Value = TextBox2.Text;
Response.Cookies["UName"].Expires = DateTime.Now.AddMonths(2);
Response.Cookies["PWD"].Expires = DateTime.Now.AddMonths(2);
}
Session["FacultyId"] = TextBox1.Text;
Session.Add("FacultyFisrtName", name);
FormsAuthentication.RedirectFromLoginPage(name, false);
Debug.Write(Session["FacultyFisrtName"].ToString());
}
}
if (DropDownList1.SelectedItem.Text == "Non-Faculty Staff")
{
SqlConnection con = new SqlConnection(strcon);
SqlCommand cmd = new SqlCommand("Select AccEmployeeName from AccEmployee where AccEmployeeId=@aid and Password=@pw", con);
cmd.Parameters.AddWithValue("@aid", TextBox1.Text);
cmd.Parameters.AddWithValue("@pw", TextBox2.Text);
con.Open();
string name = Convert.ToString(cmd.ExecuteScalar());
con.Close();
if (String.IsNullOrEmpty(name))
Label1.Text = "Sorry! Invalid User ID or Password!";
else
{
if (CheckBox1.Checked)
{
Response.Cookies["UName"].Value = TextBox1.Text;
Response.Cookies["PWD"].Value = TextBox2.Text;
Response.Cookies["UName"].Expires = DateTime.Now.AddMonths(2);
Response.Cookies["PWD"].Expires = DateTime.Now.AddMonths(2);
}
Session["AccEmployeeFacultyId"] = TextBox1.Text;
Session.Add("AccEmployeeName", name);
FormsAuthentication.RedirectFromLoginPage(name, false);
Debug.Write(Session["AccEmployeeName"].ToString());
}
}
if (DropDownList1.SelectedItem.Text == "Admin")
{
SqlConnection con = new SqlConnection(strcon);
SqlCommand cmd = new SqlCommand("Select AdminName from Admin where AdminId=@pid and Password=@pw", con);
cmd.Parameters.AddWithValue("@pid", TextBox1.Text);
cmd.Parameters.AddWithValue("@pw", TextBox2.Text);
con.Open();
string name = Convert.ToString(cmd.ExecuteScalar());
con.Close();
if (String.IsNullOrEmpty(name))
Label1.Text = "Sorry! Invalid User ID or Password!";
else
{
if (CheckBox1.Checked)
{
Response.Cookies["UName"].Value = TextBox1.Text;
Response.Cookies["PWD"].Value = TextBox2.Text;
Response.Cookies["UName"].Expires = DateTime.Now.AddMonths(2);
Response.Cookies["PWD"].Expires = DateTime.Now.AddMonths(2);
}
Session["AdminId"] = TextBox1.Text;
Session.Add("AdminName", name);
FormsAuthentication.RedirectFromLoginPage(name, false);
Debug.Write(Session["AdminName"].ToString());
}
}