Silvia Stoyanova
Silvia Stoyanova

Reputation: 281

Save an already created object in session

I am trying to create an object and then save it in a Session, then redirect to another page and use the object saved in the Session.

Here's my code, which seems to be wrong somewhere because it's not operational.

PAGE 1

 public FitnessClassOpportunity GetData()
    {

        return new FitnessClassOpportunity(txtId.Text, txtDescription.Text, txtLocation.Text, 
                                           Convert.ToInt32(tx2.Text), dropDownDay.SelectedItem.ToString(), 
                                           txtTime.Text, Convert.ToInt32(txtDuration.Text), CheckBox1.Checked, 
                                           txtDatecompleted.Text, txtNumSession.Text);

    }



 protected void Button1_Click(object sender, EventArgs e)
    {
//result from breaking point: f has the data
            FitnessClassOpportunity f = GetData();
//result from breaking point: f still has the data but Session is still NULL
        Session["object"] = f;
         Response.Redirect("Default.aspx");
    }

PAGE 2

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {

        }
        else 
        {
//add the object from the session to a list
      fitnessClassList.addFitnessClass((FitnessClassOpportunity)Session["object"]);
            UpdateListbox();
        }
      }

Upvotes: 0

Views: 753

Answers (1)

slfan
slfan

Reputation: 9139

If you do a redirect it is not a postback any longer and the code to add the object to the list is not executed. Run a debugger and you will see.

EDIT: Maybe session state is disabled. Set this in your web.config (should be default):

<system.web>
  <sessionState mode="InProc"/>
  ...
</system.web>

Upvotes: 1

Related Questions