1

some variables in my class:

1-companyID

2-CompanyName(UserName)

3-CompanyPwd(password)

4- CompanyLogo

5-Email.

i wana create session content all of these fields to use some of them in ctrls.

1 Answer 1

2

Store the class object in session

public class Company
{
  public int Id { set;get;}
  public string Name { set;get;}
  public string Logo { set;get;}  
}

To store it in session

Company objCompany=new Company();
objCompany.ID=34;
objCompany.Name="Chase";
objCompany.Logo="chase_logo.PNG";

Session["company"]=objCompany;

To Retrieve from Session,

Company objComp=null;
if(Session["company"]!=null)
{
  objComp=(Company) Session["company"];
}

Ideally I would wrap this in a method like this

public Company GetCurrentCompany()
{
    Company objComp=null;
    if(Session["company"]!=null)
    {
      objComp=(Company) Session["company"];
    }
}

so that i can simply call GetCurrentCompany method if i want the stored company in different places.

Sign up to request clarification or add additional context in comments.

4 Comments

alright now how can i use session in other place as ctrl ?? and how can i retrieve one field from session?
@Alaahafethburghall ctrl means ?
Control i meant any page how to call the session, because ive used the session depends on function called Find to find everything in the class so how can i call one value as "CompanyName" in control page
@Alaahafethburghall: Session will be available in all the pages as long as the user session exist. So you can use the way i mentioned in the answer (To Retrieve from Session part)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.