title: Storing a Complex Object in Session State
body: <p>I have a separate class under my App_Code folder (.NET project) called "StateBag.cs"</p>
  
  <p>using System;
  using System.Text;</p>
  
  <pre><code>[Serializable()]
  public class MyStateBag
  {
      public MyStateBag(){}
      private string _MemberID = string.Empty;
      public string MemberID
      {
          get { return _MemberID; }
          set { _MemberID = value; }
      }
  }
  </code></pre>
  
  <p>Now I want to be able to update the MemberID value from any page of my web project.</p>
  
  <p>example:-</p>
  
  <p>Default.aspx.cs:-</p>
  
  <pre><code>  public partial class _Default : System.Web.UI.Page
    {
      public StateBag MyStateBag
      {
          get { return (StateBag)Session["MyStateBag"]; }
          set { Session["MyStateBag"] = value; }
      }
  
      protected void Page_Init(object sender, EventArgs e)
      {
          this.MyStateBag = (StateBag)Session["MyStateBag"];
      }
  
      .... }
  </code></pre>
  
  <p>On my Default.aspx.cs Page I am trying to set the value of memberID into MyStateBag:-</p>
  
  <pre><code> if (HttpContext.Current.Session["MyStateBag"] == null)
          {
              HttpContext.Current.Session["MyStateBag"] = new StateBag();
          }
          ((StateBag)HttpContext.Current.Session["MyStateBag"]).MemberID = memID;
  </code></pre>
  
  <p>However I get a conflict:-
  <strong>'System.Web.UI.StateBag' does not contain a definition for 'MemberID' and no extension method 'MemberID' accepting a first argument of type 'System.Web.UI.StateBag' could be found (are you missing a using directive or an assembly reference?)</strong></p>
  
  <p>Do I have to reference something on my Default.aspx.cs page that I am missing out on??</p>
  
  <p>and how can i access it back...say I am on a different page,"About.aspx.cs".</p>
  
  <p>I want to be able to say :-<code>memberinfo = StateBag["MemberID"]</code></p>
  
  <p>Can anyone help me understand this?</p>