Kevin Fauver
Kevin Fauver

Reputation: 581

c# class in a session state?

My senior project is building a reservation system in ASP.NET/C#. Part of my senior project is to have c# classes (and basically use everything ive learned in the past few years). One thing Im trying to do is after I instantiate a new "user" class I need it to travel between the pages. I know session states holds variables, so I figured a session state would work where I can simply type "Session["blah"]." and have access to its members. But I dont see that happening. I realize session states are HTTP context, so i doubted it would work anyway. But is there any other way in which I can accomplish what I need without instantiating a new user class every time? I know its a webpage...but im also trying to make it as much as a functional online program as I can.

Just for coder's sake, heres the code snippet im working with:

    cDatabaseManager cDM = new cDatabaseManager();

    string forDBPass = Encryptdata(pass_txt.Text.ToString());
    string fullName = fname_txt.Text.ToString() + " " + lname_txt.Text.ToString();

    cDM.regStudent(email_txt.Text.ToString(), forDBPass, fullName, num_txt.Text.ToString(), carrier_ddl.SelectedValue.ToString(), this);

    //ADD - getting a cStudent
    cUser studentUser = new cStudent(fullName, forDBPass, email_txt.Text.ToString());

    //ADD - session states
    Session["cStudent"] = studentUser;

    //Session["cStudent"].      //session state will not work with what I am doing
    //ADD - transfer to campus diagram

Thanks in advance!!

EDIT:

I want to thank all of you who posted and commented! Ive learned alot from this short discussion. All your answers helped me understand!

Upvotes: 0

Views: 2013

Answers (3)

MNVR
MNVR

Reputation: 875

When retrieving an object value from session state cast it to appropriate type.

[Serializable]    
public class student
    {
      public string FirstName { get; set; }
      public string LastName { get; set; }
    }

In Page1:

student s1 = new student();
s1.FirstName ="James";
s1.LastName = "Bond";
Session["blah"] = s1;

And when you want to access Session["blah"] in page 2

student s2 = (Session["blah"] !=null ? (student)Session["blah"] : null);

Now you can access properties of s2 as s2.FirstName, s2.LastName

Upvotes: 2

Shai Cohen
Shai Cohen

Reputation: 6249

Session stores item as objects. As long as your class inherits from Object (which it does) you can store it there. Quick caveat, it stores that object using Serialization, so your class must be serializable.

Add a property to your class like so:

public cStudent CurrentStudent
{
    get {
        if(Session["CurrentUser"] == null)
            return null;

        return (cStudent)Session["CurrentUser"];
    }
    set {
        Session["CurrentUser"] = value;
    }
}

Upvotes: 4

D Stanley
D Stanley

Reputation: 152606

From your comment:

The issue is when I type "Session["cStudent"]." I don't have access to my functions. Example: Session["cStudent"].getName() does not give my functionality.

This is because the [] indexer for Session sets/returns objects. The compiler does not know that you stored a cUser object and so you can't access the properties directly without a cast:

string name = ((cUser)Session["cStudent"]).getName();

There are two things that could go wrong here:

  1. If Session["cStudent"] is null you will get a NullReferenceException
  2. If Session["cStudent"] is not really a cUser you will get an InvalidCastException

You should check these conditions and react appropriately if one of them is true.

Also, as others have pointed out, the cUser class needs to be marked as Serializable in order to be stored in Session state.

Upvotes: 4

Related Questions