登录按钮:
 protected void btnLogin_Click(object sender, EventArgs e)
    {
        LoginEntity le = new LoginEntity();
        le.UserName = this.txtUserName.Text;
        le.UserIdentity = this.DropDownList1.SelectedItem.ToString();
        Session.Add("User",le);
        Application.Add("AUser",le);
        Response.Redirect("DisplayLogin.aspx");
        
    }
DisplayLogin.aspx页:
protected void Page_Load(object sender, EventArgs e)
    {
        LoginEntity le = (LoginEntity)Session["User"];
        Response.Write("用户名是:"+le.UserName+"<p>");
        Response.Write("身份是:" + le.UserIdentity + "<p>");
        Response.Write(Application.Count.ToString());
        for (int i = 0; i < Application.Count; i++)
        {
            LoginEntity le1 = (LoginEntity)Application["AUser"];
            //LoginEntity le1 = (LoginEntity)Application["AUser"];
            Response.Write("在线用户:+<p>" + le1.UserName + "&nbsp;&nbsp;身份是:" + le1.UserIdentity + "<p>");
        }
    }提示此句:LoginEntity le1 = (LoginEntity)Application["AUser"];
这个错误:无法将类型为“System.String”的对象强制转换为类型“LoginEntity”。

解决方案 »

  1.   

    你在你的解决方案里查找下Application["AUser"],看是不是还有地方对这个赋值了,一看就知道在你当前逻辑之外的地方对这个键值赋值了string类型的值,导致了你现在的错误。
    知道怎么在解决方案里全盘查询不?
      

  2.   

    谢谢你的关注,我解决了. Application.Add("AUser",le); 改成:Application["AUser"]=le;
    就行了,不过又出现了一个问题:就是如何显示出在线用户列表呢,我刚上边的代码也是想实现这个功能,但没实现得了.
     
      

  3.   


    public static class LoginUserManager
    {
    private static Dictionary<string, LoginEntity> UserCollection
    {
    get
    {
    if(Application["AUser"] == null)
    {
    Application["AUser"] = new Dictionary<string,LoginEntity>();
    }
    return Application["AUser"] as Dictionary<string,LoginEntity>;
    }
    }public static void Add(LoginEnity loginEntity)
    {
    Dictionary<string,LoginEntity> collection = UserCollection;
    if(collection.ContainsKey(loginEntity.UserName))
    {
    collection[loginEntity.UserName] = loginEntity;
    }
    else
    {
    collection.Add(loginEntity.UserName,loginEntity);
    }
    }public static LoginEntity[] GetAll()
    {
    return UserCollection.Values.ToArray();
    }
    }
    登录按钮: 
    protected void btnLogin_Click(object sender, EventArgs e) 
        { 
            LoginEntity le = new LoginEntity(); 
            le.UserName = this.txtUserName.Text; 
            le.UserIdentity = this.DropDownList1.SelectedItem.ToString(); 
            Session.Add("User",le); 
            LoginUserManager.Add(le); 
            Response.Redirect("DisplayLogin.aspx"); 
            
        } 
    DisplayLogin.aspx页: 
    protected void Page_Load(object sender, EventArgs e) 
        { 
            LoginEntity le = (LoginEntity)Session["User"]; 
            Response.Write("用户名是:"+le.UserName+" <p>"); 
            Response.Write("身份是:" + le.UserIdentity + " <p>"); 
            Response.Write(Application.Count.ToString()); 
            LoginEntity[] entitys = LoginUserManager.GetAll();
            foreach(LoginEntity le1 in entitys)
            { 
                Response.Write("在线用户:+ <p>" + le1.UserName + "&nbsp;&nbsp;身份是:" + le1.UserIdentity + " <p>"); 
            } 
        } 这是我刚刚在回复框里手写的,可能少些引用以及有些语法错误,你修正下就可以了,然后这个类不是线程安全的,你最好自己封装一个线程安全的dictionary,替换掉里面的就可以了。大致做法就是这样子的,
    建议你对于Application啊Session什么的操作用类来管理起来,不然就会出现你现在的错误,数据会乱掉。你刚刚说找到错误了,我觉得你没找到真正的错误,如果Application["AUser"]= le,能运行,说明已经有了这个键值了,只是你直接赋值又把原来的给覆盖掉了而已。