Tyler
Tyler

Reputation: 13

Writing multiple values to session variable in C#

Does anyone know how to write multiple values to a single session variable in C#. I have two textboxes, both will allow users to input numbers. I want to put both numbers in a single session variable. For example Textbox1 = 4, and textbox2 = 6.

Would I for example, convert both to variables then have something similar to below? If anyone could help, I would greatly appreciate it. Thanks.

Session["AppNum_Session"] = txtbox_var1, txtbox_var2;

Upvotes: 1

Views: 1895

Answers (1)

Juan Ruiz de Castilla
Juan Ruiz de Castilla

Reputation: 974

Create a class and add to session.

public class Values
{
   public int value_1 { set; get; }
   public int value_2 { set; get; }
}

Session["values"] = new Values() { value_1 = 1, value_2 = 2 };

Then when you want get the class use this:

Values values = (Values)Session["values"];
lbltest.Text = values.value_1.ToString();

Upvotes: 1

Related Questions