c# - Retrieve Specific Data from Session List -


i stored list in session. able username list accessing userinfo[1]. after stored in session, unable session["userinfo"][1].

it gave me error

"cannot apply indexing [] expression of type object.

any idea/hints me this?

list<string> userinfo = new list<string>(); userinfo.add(userid); userinfo.add(username); userinfo.add(role); session[userinfo"] = userinfo; 

you need cast list first because default session return type object, this:

var theuserinfo = session["userinfo"] list<string>;  // checking if indeed list of string if(theuserinfo != null) {    // here list.. } 

or can implicitly cast this:

userinfo = (list<string>) session["userinfo"] 

as additional notes, if explicitly cast object, there's chance can invalid cast exception. so, recommend use "classic" approach use as syntax, because return null rather throw exception.


Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -