Subject: Accessing Strongly Typed Session Data
Posted By: Woodman Post Date: 3/8/2007 11:12:12 AM
I have your Professional ASP.NET 2.0 book. Very nice. I am implementing a StateServer to manage session data. I used your samples in the book as a model, but all data stored was string data. My implementation stores int data. Everything has compiled, and I can assign int values to the Session data, but I can't seem to format the syntax appropriately to extract it. The following forms give "Specified cast not valid":

int foo = (int)Session["openproject"];
or
int foo = Convert.Int32(Session["openproject"].ToString());

I have implemented a serializable class with private "int" variables for the session object. Help!

Tom


Reply By: Woodman Reply Date: 3/8/2007 6:13:01 PM
Here is the class definition...

[Serializable]
public class Project
{
    public int projectid;

    public Project()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    public override string ToString()
    {
        return String.Format("Project Object: {0:c}}", projectid);
    }

}

Here is the get/set implemented within the System.Web.UI.Page subclass...

    public Project OpenProject
    {
        get
        {
            return (Project)Session[OPENPROJECT];
        }
        set
        {
            Session[OPENPROJECT] = value;
        }
    }



Reply By: Woodman Reply Date: 3/9/2007 3:42:07 PM
Nevermind. Found the problem. I'm stupid...

this.OpenProject.projectid




Go to topic 30391

Return to index page 6
Return to index page 5
Return to index page 4
Return to index page 3
Return to index page 2
Return to index page 1