我有四个学生,教师,非教师和管理员的数据库,在登录页面上我有一个用户ID和密码文本框用于输入加上用户类型下拉列表...我在其中添加了他们的名字会话登录页面,显然当我在我的输出控制台和主页上调试它时,它显示了所有四个用户的名字,因此我想会话也在那里创建...我的主页代码如下:
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 .. .how我能解决这个例外吗?并问我是否需要更多信息.... :)
herez我的提交按钮代码:
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());
}
}