Search code examples
c#asp.netencapsulation

Storing Class objects into Session and displaying on another asp.net Webform


Using asp.net, I am trying to retrieve textbox input, encapsulate this data in a C# class, store the class object in a session and display this data on another Web Form. My thinking is that I need to set the properties in the C# class Customer and then store the whole class object into a Session but maybe I am going about this wrong.

Here is a portion of my asp.net code that contains the textboxes:

  <asp:Label ID="lblName" runat="server" Text="Name: "></asp:Label>
                <br />
                <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
                <br /><br />
                <asp:Label ID="lblAddress" runat="server" Text="Address: "></asp:Label>
                <br />
                <asp:TextBox ID="txtAddress" runat="server"></asp:TextBox>
                <br /><br />
                <asp:Label ID="lblPhoneNumber" runat="server" Text="Phone Number: "></asp:Label>
                <br />
                <asp:TextBox ID="txtPhoneNumber" runat="server"></asp:TextBox>

Here is my code behind:

protected void btnSubmitPersonalInfo_Click(object sender, EventArgs e)
        {
            Customer customer = new Customer();
            customer.Name = txtName.Text;
            customer.Address = txtAddress.Text; 
            customer.PhoneNumber = txtPhoneNumber.Text;
            customer.City = txtCity.Text;
            customer.State = txtState.Text; 
            customer.Zip = txtZip.Text;

            Session["Customer"] = customer;
            Response.Redirect("CreditCardInfo.aspx");
        }

And here is my C# class:

public class Customer
    {
        public string Name { get; set; }
        public string Address { get; set; }
        public string PhoneNumber { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string Zip { get; set; }
        public string CardNumber { get; set; }
        public string ExpirationDate { get; set; }
        public string SecurityCode { get; set; }
        public Customer() { }

    }    

And this is the web form where I want to display the data:

    public partial class DisplayData : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        txtDisplayData.Text = Session["Customer"].ToString();

    }
}

Currently, txtDisplayData is displaying the namespace and that is it. Can someone please point me in the right direction as I feel like I am overthinking this. Thanks.


Solution

  • You can store anything in sessions. To retrieve back their properties, convert the session to the original class. It should works.

    In your case, you're directly providing your session, you need to convert it back to the Class and then provide it:

    Customer customer = new Customer();
    // Assign object into session
    Session["Customer"] = customer ;
    

    And you can get details like this:

    // get session value into object
    Customer customer =  (Customer)Session["Customer"];