Asp.net 使用Session为多个用户创建登录名时出现错误,并进一步评估用户的权限

Asp.net 使用Session为多个用户创建登录名时出现错误,并进一步评估用户的权限,asp.net,session,Asp.net,Session,我尝试过很多方法,但它只是显示错误“对象引用未设置为对象的实例” 描述:执行当前web请求期间发生未处理的异常。请查看堆栈跟踪以了解有关错误的更多信息以及错误在代码中的起源 异常详细信息:System.NullReferenceException:对象引用未设置为对象的实例 源错误: else if (Session["StudId"] != null) { Label1.Text = Session[

我尝试过很多方法,但它只是显示错误“对象引用未设置为对象的实例”

描述:执行当前web请求期间发生未处理的异常。请查看堆栈跟踪以了解有关错误的更多信息以及错误在代码中的起源

异常详细信息:System.NullReferenceException:对象引用未设置为对象的实例

源错误:

                 else if (Session["StudId"] != null)
             {
                 Label1.Text = Session["StudId"].ToString();
             }
我已经在我的登录页面中编写了此代码,并在页面中拖拽了所有必需的数据库字符串,即typeid、students、faculty、admin和accemployee

public partial class Login : System.Web.UI.Page
 {private string strcon = WebConfigurationManager.ConnectionStrings["StudentConnectionString1"].ConnectionString;

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        if (Request.Cookies["UName"] != null)
            TextBox1.Text = Request.Cookies["UName"].Value;
        if (Request.Cookies["PWD"] != null)
            TextBox2.Attributes["value"] = Request.Cookies["PWD"].Value;
        if (Request.Cookies["UName"] != null && Request.Cookies["PWD"] != null)
            CheckBox1.Checked = true;
    }
}
protected void Button1_Click1(object sender, EventArgs e)
{

    if (DropDownList1.SelectedItem.Value == "1")
    {
        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);
            Session.Add("Password", TextBox2.Text);
            FormsAuthentication.RedirectFromLoginPage(name, false);
        }
    }





      else if (DropDownList1.SelectedItem.Value == "2")
    {
        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);
            Session["Password"] = TextBox2.Text;
            FormsAuthentication.RedirectFromLoginPage(name, false);
        }
    }
    else if (DropDownList1.SelectedItem.Value == "3")
    {
        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);
            Session["Password"] = TextBox2.Text;
            FormsAuthentication.RedirectFromLoginPage(name, false);
        }
    }
    else if (DropDownList1.SelectedItem.Value == "4")
    {
        SqlConnection con = new SqlConnection(strcon);
        SqlCommand cmd = new SqlCommand("Select  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);
            }
            string adminName = "Pujan";
            Session["AdminId"]=TextBox1.Text;
            Session["AdminName"] = adminName;
            Session["Password"]=TextBox2.Text;
            FormsAuthentication.RedirectFromLoginPage(name, false);
        }
    }

}


protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    Label2.Text = DropDownList1.SelectedItem.Text;

}
` }

徖。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 现在错误出现在masterpage.master.cs中,如下所示

  public partial class MasterPage : System.Web.UI.MasterPage
{
   protected void Page_Load(object sender, EventArgs e)

{

            if (Session["StudId"] == null)
            Response.Redirect("Login.aspx");

            else if (Session["StudId"] != null)
            {
                Label1.Text = Session["StudId"].ToString();
            }


            else if (Session["FacultyFirstName"] == null)
            {
                Response.Redirect("Login.aspx");
            }

            else if (Session["FacultyFirstName"] != null)
            {
                Label1.Text = Session["FacultyFirstName"].ToString();
            }

            else if (Session["AccEmployeeName"] == null)
            {
                Response.Redirect("Login.aspx");
            }

            else if (Session["AccEmployeeName"] != null)
            {
                Label1.Text = Session["AccEmployeeName"].ToString();
            }

            else if (Session["AdminName"] == null)
            {
                Response.Redirect("Login.aspx");
            }

            else if (Session["AdminName"] != null)
            {
                Label1.Text = Session["AdminName"].ToString();
            }


}



protected void LinkButton1_Click1(object sender, EventArgs e)
{
    FormsAuthentication.SignOut();
    Response.Redirect("Login.aspx");
}

}
请建议我如何消除会话或wateva中的错误……提前谢谢:)

System.NullReferenceException:对象引用未设置为对象的实例

此错误意味着提供的值为
null
,服务器无法将其用于需要使用参数的进程

有时,当您试图在方法中使用变量时,会发生这种情况,而该变量会获得空值。空值意味着这个东西没有值或没有数据

在您的代码中,我猜这个错误会在站点首次加载时产生。此时,服务器没有要加载或处理的会话。因此,这些值都是null,抛出这个null异常

您可以尝试在
if-else
块中隐藏代码,以检查会话是否存在cookie,或者尝试
try-catch
块以最小化此异常,并根据情况执行工作

例如:

try {
   /* your code here */
} catch (System.NullReferenceException) {
   /* create a session or fill up the variable */
}
这个块将运行您的代码,如果抛出Catch方法中提供的异常,它将在Catch块中执行代码

第二件事是使用if-else:

if(variable != null) {
   /* your code here */
} else {
   /* set the value */
}
您只需检查该特定变量的值,然后进行检查。如果它是一个空值变量,那么您可以跳过代码块的执行,用一个值填充变量,然后返回到当前空间并重新执行它


有关异常详细信息:

请参阅。最有可能的
Label1
null
。我还是不明白…我的意思是我不知道如何使用嵌套形式的try-and-catch…你能帮我一下吗,兄弟?当然,但我能告诉你的是,你可以将所有代码包装在
try
块中。然后,使用
catch
块捕获所有异常……我已经看到了,这是行不通的。我想你首先需要学习语言的语法!然后你可以继续。。。