locked
Session - data container RRS feed

  • Question

  • User-720198320 posted

    Hello.
    Please help.
    I created the data container in a public class.
    And now I need to use a session variable.
    I have a form where I have 3 inputs.
    I need to use the forum containers for storing data and also save into the session, but as an object from the data forum containers.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    namespace TestProject
    {
        public class NameDC
        {
            public int CustID
            {
                get;
                set;
            }
    
            public string Name
            {
                get;
                set;
            }
    
    
        }
    }

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:TextBox ID="txtId" runat="server"></asp:TextBox><br />
            <asp:TextBox ID="txtName" runat="server"></asp:TextBox><br />
            <asp:Button ID="btnSaveToSessionAsObject" runat="server" Text="Button" onclick="btnSaveToSessionAsObject_Click" />
        </div>
        </form>
        
    </body>
    </html>

    protected void btnSaveToSessionAsObject_Click(object sender, EventArgs e)
    {
        (NameDC)Session["Name"] = txtId.Text; //Please help ??
        //You need to input the data from the store in a session variable. Using object   //NameDC do not know how. Please help. How do you pop the date of the     
    //session using the session object?
    
    }





    How do I save a session with the data container?
    To use the session as an object on another page?
    Sorry for my English.

    Monday, December 20, 2010 2:24 AM

Answers

  • User-68639941 posted

    hi, refer below code

    NameDC names=new NameDC();

    names.CustID=txtId.Text;

    names.Name=txtName.Text;

    Session["Name"] =names;

    to retrieve values

    NameDC names=Session["Name"] as NameDC ;

    names.CustID=txtId.Text;

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 20, 2010 4:00 AM
  • User1572535980 posted

    Do you mean like this:

        NameDC  dc = new NameDC();
        try { dc.CustID = int.Parse(txtId.Text);} catch {dc.CustID = 0;}    
        dc.Name = txtName.Text;
        (NameDC)Session["NameDC"] = dc;


    and other side:

    NameDC dc = new NameDC();

    dc = Session["NameDC"];
     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 20, 2010 4:12 AM
  • User1572535980 posted

    ups,

    Session["NameDC"] = dc;  

    and other side:

    dc = (NameDC)Session["NameDC"];

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 20, 2010 4:22 AM
  • User1572535980 posted

    I made error in first post, this is right: Session["NameDC"] = dc; 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 20, 2010 4:57 AM

All replies

  • User-68639941 posted

    hi, refer below code

    NameDC names=new NameDC();

    names.CustID=txtId.Text;

    names.Name=txtName.Text;

    Session["Name"] =names;

    to retrieve values

    NameDC names=Session["Name"] as NameDC ;

    names.CustID=txtId.Text;

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 20, 2010 4:00 AM
  • User1572535980 posted

    Do you mean like this:

        NameDC  dc = new NameDC();
        try { dc.CustID = int.Parse(txtId.Text);} catch {dc.CustID = 0;}    
        dc.Name = txtName.Text;
        (NameDC)Session["NameDC"] = dc;


    and other side:

    NameDC dc = new NameDC();

    dc = Session["NameDC"];
     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 20, 2010 4:12 AM
  • User1572535980 posted

    ups,

    Session["NameDC"] = dc;  

    and other side:

    dc = (NameDC)Session["NameDC"];

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 20, 2010 4:22 AM
  • User-720198320 posted

    Please help.

    Error: The left-hand side of an assignment must be a variable, property or indexer

    (NameDC)Session["NameDC"] = dc;


    Monday, December 20, 2010 4:41 AM
  • User1572535980 posted

    I made error in first post, this is right: Session["NameDC"] = dc; 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 20, 2010 4:57 AM
  • User-720198320 posted

    Thank you. Smile

    Monday, December 20, 2010 5:00 AM