0

I have some value related to Particular Name, for e.g.:

FirstName : Ghanshyam;
LastName : Thakkar;
Age : 25;
Designation : Student;

I want to pass this value to another Page using Session. How can I pass both Name and Value both in one variable using Session?

1
  • 1
    what if you go for JSON / XML ???? Commented Feb 10, 2012 at 6:53

3 Answers 3

5

Build a 'Person' class, fill that and add to Session.

[Serializable]
public class Person
{
   public string FirstName{get;set;}
   public string LastName {get;set;}
   public int Age {get; set;}
   public string Designation {get;set;}
}

usage:

Person thePerson = new Person();
thePerson.FirstName = "Ghanshyam";
thePerson.LastName = "Thakkar";
thePerson.Age = 25;
thePerson.Designation = Student;

Session["ThePerson"] = thePerson;

And in the other page:

Person thePerson = (Person)Session["ThePerson"];
Sign up to request clarification or add additional context in comments.

2 Comments

-1 for the fact that if you don't mark the class as [Serializable you cannot store if to session.
@Floradu88 - If it's an InProc Session, it will still work, but I'll add it.
2

Have you tried : http://msdn.microsoft.com/en-us/library/ms972429.aspx ?

This one is good too : http://asp.net-tutorials.com/state/sessions/

As for sending them in one variable : use [Serializable] class and put properties for all the info you need to be passed and store that object in session.

Comments

1

Try this .. Initialize session by

Session["value"] = FirstName +":"+LastName ;

on the other side of page use split

var sessionvalue = Session["value"].toString().Split(':');

finally refer using

sessionvalue[0],sessionvalue[1]

2 Comments

You should better use defensive programming to get that object and cast your session object to string , after checking it's type.
yes @Floradu88 u are right too... but even this works in simple case when without classes ...

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.