Reputation: 487
reader = server.ExecuteReader(CommandType.Text, getPermissionlistQuery, param);
var results = new List<string>();
while (reader.Read())
{
results.Add(reader["permissionName"].ToString());
}
reader.Close();
Session.Add("Permissions", results);
I am adding results to the session, how can I retrieve it in another page.
results is a list of values
var permissionList = Session["Permissions"];
string check = "Create Groups";
if (permissionList.Any(item => item.Equals(check)))
{
// results contains the value in check
}
and I want to check whether the permission is available in the Permission list but the if statement is throwing a error
'object' does not contain a definition for 'Any' and no extension method 'Any' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)
Upvotes: 0
Views: 1453
Reputation: 8598
UPDATED: Change bottom code to this:
IList<String> collection = (IList<String>) Session["Permissions"];
string check = "Create Groups";
if (collection.Any(item => item.Equals(check)))
{
// results contains the value in check
}
Also, reference:
using System.Collections.Generic;
using System.Linq;
Upvotes: 1
Reputation: 7268
Looks like permissionList is getting populated i.e. the code to pull data from session is working fine. If this wouldn't have been the case, then you would have received NullReference exception.
My guess here is that the real culprit is "var" keyword. When you pull PermissionList from session, it is retrieved as an "Object". Try explicitly casting the permissionList object i.e.
List<string> permissionList = Session["Permissions"];
or
var permissionList = (List<string>)Session["Permissions"];
Upvotes: 0
Reputation: 6111
Just call String value = (String)Session["Key"];
to get the value
Session["Key"]
will pull the value in session for that key. If the key doesn't exist it will return Null.
You may want to read up on ASP.NET Session as well.
In response to your edit, ensure you have references to Linq for your project. Any is a Linq extension.
Upvotes: 1