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
Post a Comment