从会话对象获取值

时间:2015-10-15 13:37:56

标签: c# asp.net session

这是我设置会话对象的代码。我想使用该对象来获取登录用户的信息。但我无法做到这一点我得到错误'输入字符串格式不正确'。我需要帮助,知道如何创建一个对象&从我的会话对象中获取值

public UserLoginDetails LoggedUserDetails
    {
        set { Session["loggeduserdetails"] = value; }
        get
        {
            if (Session["loggeduserdetails"] == null)
            {
                if (Session["username"] == null)
                {
                    Response.Redirect("../Authentication/Login.aspx");
                }

                if (Session["password"] == null)
                {
                    Response.Redirect("../Authentication/Login.aspx");
                }

                DAL.LoginHistoryDAL LoginDAL = new DAL.LoginHistoryDAL();

                DataSet Ds = LoginDAL.AuthenticateUser(Session["username"].ToString(), EncryptionUtility.Encrypt(Session["password"].ToString()));

                UserLoginDetails ULD = new UserLoginDetails();

                if (Ds.Tables[0].Rows.Count > 0)
                {
                    // Populating UserObj
                    bool IsAdmin = false;
                    bool IsMasterAdmin = false;

                    if (Ds.Tables[0].Rows[0]["IsAdmin"].ToString() == "1")
                    {
                        IsAdmin = true;
                    }
                    else
                    {
                        IsAdmin = false;
                    }

                    if (Ds.Tables[0].Rows[0]["IsMasterAdmin"].ToString() == "1")
                    {
                        IsMasterAdmin = true;
                    }
                    else
                    {
                        IsMasterAdmin = false;
                    }


                    ULD = new UserLoginDetails();

                    ULD.UserId = int.Parse(Ds.Tables[0].Rows[0]["UserId"].ToString());
                    ULD.IsAdmin = IsAdmin;
                    ULD.IsMasterAdmin = IsMasterAdmin;
                    ULD.UserName = Ds.Tables[0].Rows[0]["FullName"].ToString();
                    ULD.LocationID = int.Parse(Ds.Tables[0].Rows[0]["LocationID"].ToString());
                    ULD.FullName = Ds.Tables[0].Rows[0]["FullName"].ToString();
                    ULD.StatusId = int.Parse(Ds.Tables[0].Rows[0]["StatusId"].ToString());
                    ULD.Email = Ds.Tables[0].Rows[0]["Email"].ToString();
                    ULD.CellNo = Ds.Tables[0].Rows[0]["Cell"].ToString();
                    ULD.CityId = int.Parse(Ds.Tables[0].Rows[0]["CityID"].ToString());
                    ULD.StateId = int.Parse(Ds.Tables[0].Rows[0]["StateId"].ToString());
                    ULD.CountryId = int.Parse(Ds.Tables[0].Rows[0]["CountryID"].ToString());
                }

                Session["loggeduserdetails"] = ULD;

            }
            return (UserLoginDetails)Session["loggeduserdetails"];
        }
    }

}
public class UserLoginDetails
{
    public int UserId = 0;
    public bool IsAdmin = false;
    public bool IsMasterAdmin = false;
    public string UserName = string.Empty;
    public int LocationID = 0;
    public string FullName = string.Empty;
    public int StatusId = 0;
    public string Email = string.Empty;
    public string CellNo = string.Empty;
    public int CityId = 0;
    public int StateId = 0;
    public int CountryId = 0;
}

1 个答案:

答案 0 :(得分:0)

看起来您期望成为数值的数据之一不是。 即UserId或CityID不是数值或任何其他值。 我将看一下以下代码行之一:

ULD.UserId = int.Parse(Ds.Tables[0].Rows[0]["UserId"].ToString());
ULD.LocationID = int.Parse(Ds.Tables[0].Rows[0]["LocationID"].ToString());
ULD.StatusId = int.Parse(Ds.Tables[0].Rows[0]["StatusId"].ToString());
ULD.CityId = int.Parse(Ds.Tables[0].Rows[0]["CityID"].ToString());
ULD.StateId = int.Parse(Ds.Tables[0].Rows[0]["StateId"].ToString());
ULD.CountryId = int.Parse(Ds.Tables[0].Rows[0]["CountryID"].ToString());