Click here to Skip to main content
15,919,422 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello guys..im newbie in ASP.NET. Using Session, i want to access variable of one page into another how do I do that. Here is what I am trying which is not working

public class Student{
   string name;
   string Class;

   public Student(string n, string c){
       name = n;
       class = c;
   }
}

Now in the Page_Load() event of Default.aspx.cs im creating two objects of Student and adding them to the Session (and to the ListBox like this)
Student student1 = new Student("Muzammil","MS");
Student student2 = new Student("Ali","MBA");

//adding these objects to Session
Session["std1"] = student1;
Session["std2"] = student2;

//adding these objects' name to list box
list1.Items.Add(student1.name);
list1.Items.Add(student2.name);

Now in the button's event in Default.aspx.cs im accessing these as follows
int index = list1.SelectedIndex;
Student std = (Student)Session[index];

lblRecords.Text = "Name:  " + std.name;
lblRecords.Text = "Class:  " + std.Class;

Now there is another page "Page1.aspx" with a label on it. How do I access these variable on that label? thnx
Posted

If any one visit Page1.aspx page 1st then you can not get the value in session. So, you should check 1st that values are available in the session.

if (Session["std1"] != null && Session["std2"] != null)
{
Student std1 = Session["std1"] as Student;
Student std2 = Session["std2"] as Student;
}
 
Share this answer
 
Comments
AmbiguousName 6-Oct-10 14:34pm    
As you can see that I am using index as key (returned by list1.SelectedIndex), now this is not accessible on the other page. It is only accessible only in the current page, so how can I get this key for other page
Data is stored in session in Key, Value format. As you are doing already.

To acces any particular data, you must know the key.

So on any page, you can access the data as
Student student1 = (Student)Session["std1"];
Student student2 = (Student)Session["std2"];
 
Share this answer
 
Comments
AmbiguousName 6-Oct-10 14:33pm    
As you can see that I am using index as key (returned by list1.SelectedIndex), now this is not accessible on the other page. It is only accessible only in the current page, so how can I get this key for other page.
Brij 6-Oct-10 14:40pm    
I dont know your actual requirement. You need th eindex on another page. So , you can pass the index by querystring to another page and using this key, you can fetch the value from session.
AmbiguousName 6-Oct-10 14:45pm    
thnx...since im new so it could be little confusing for me. So the conclusion is, as suggested by you, that I need to pass the index on other page by querystring.
I would suggest you to get the selected Index of list by
List<> stu_list = (List) previousPage.findControl("");
then take its selected index and get the session value.
 
Share this answer
 
Or you can use the list of students as one object in session:

C#
private const string  STUDENTS_KEY="StudentsKey"+new Guid().ToString();

public List<Student> Students
{
     get
     {
          if (Session[STUDENTS_KEY] == null)
              Session[STUDENTS_KEY] = new List<Student>();
          return Session[STUDENTS_KEY] as List<Student>;
     }
     set
     {
          Session[STUDENTS_KEY] = value;
     }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900