0

Is it possible to create a session in a class?

I am trying this in my authentication class:

HttpContext.Current.Session["user"]["name"] = "firstname";

However I get this error:

Error 342 Cannot apply indexing with [] to an expression of type 'object'

Does anyone know why?

Nima Derakhshanjan
  • 1,380
  • 9
  • 24
  • 37
Samrikan
  • 63
  • 3
  • 10

2 Answers2

3

Sessions keys/values are not stored in jagged arrays. They are accessed like a single dimension arrays:

HttpContext.Current.Session["username"] = "firstname";

Remember that sessions are stored per user at the server end so they could end up being expensive. Use them wisely.

Also consider, extracting session values in Page classes and then pass it on to your class. There is some related discussion here

Community
  • 1
  • 1
Habib
  • 219,104
  • 29
  • 407
  • 436
  • So for each value I want to store in the session I would have to create another single dimensional array? – Samrikan Apr 08 '15 at 18:00
  • @Samrikan, not sure what you meant by *another signle dimensional array*, but here is what you need to do to store other value `HttpContext.Current.Session["SomeOtherValue"] = "Value";` – Habib Apr 08 '15 at 18:02
  • Yeah so if I wanted to store the surname I would have to do this again? Is it not possible to create a multi dimensional arrayed session at all? – Samrikan Apr 08 '15 at 18:05
  • @Samrikan, oh I get it, Store your object, instead of a single value. *(where your object would have properties for FirstName, LastName etc)* Read more about it [here](https://msdn.microsoft.com/en-us/library/ms178581%28v=vs.140%29.aspx) – Habib Apr 08 '15 at 18:15
1

Classes can be added to a session. You could for example create a class:

public class User 
{
    public string FirstName { get; set ;}
    public string LastName{ get; set; }
}

Then store it in the session:

User user = new User() { FirstName = "Jon", LastName = "Doe" };
HttpContext.Current.Session["user"] = user;

Then to get the user from the session you would do:

User user = HttpContext.Current.Session["user"] as User;
string firstName = user.FirstName; // "Jon"
string lastName = user.LastName; // "Doe
RandomWebGuy
  • 1,439
  • 11
  • 23
  • If I am retrieving info from a data table and wanting to store the info from the datatable into the session separatly, would it be possible to store the row from the datatable into the session? Or is their a better alternative way for this? – Samrikan Apr 08 '15 at 18:17
  • I think technically it would be possible to store a DataRow in the session, however, I think creating a class to store the data is probably a better approach. Your method that returns the data could instead return the class. This will make it easier when you cast the session object back to the class. – RandomWebGuy Apr 08 '15 at 18:19
  • So basically send the data retrieved from the DataRow into a class then send it back to a method to store into a session? – Samrikan Apr 08 '15 at 18:28
  • If I'm understanding correctly, yes. I'd use the DataRow to populate the class' properties and then return the class rather than return a DataRow or DataTable. – RandomWebGuy Apr 08 '15 at 18:33
  • I just don't understand from your code how the User class, gets the values? – Samrikan Apr 08 '15 at 18:37
  • How it gets the values from the session? Or how it gets the values initially? – RandomWebGuy Apr 08 '15 at 18:42
  • How it gets the values initially? – Samrikan Apr 08 '15 at 18:45
  • It doesn't. It's just a simple class. You tell it what the values are. So for example if your results are in a `DataRow` after creating an instance of the class you'd do `user.FirstName = dataRow["firstName"]` – RandomWebGuy Apr 08 '15 at 18:46
  • Okay I understand, honestly thank you for your help! If I was to get the user from the session just like how you did in your code, would it appear as "JonDoe" in one string or would it be separated? – Samrikan Apr 08 '15 at 18:52
  • I updated my answer. The `Session` would contain the class `User`. You'd then access the properties like any other class. – RandomWebGuy Apr 08 '15 at 18:55