Solved

ASP.NET C# How to save many values in the Session variables?

Posted on 2011-02-17
12
1,418 Views
Last Modified: 2012-08-13
in the first page of my website,
I have some options that allow user to select...
says... a list of company....
user can check which company they are interested....

and then they click on any ohter pages
those pages are only show the companies that they have selected in the first page....

how can I do that?

Hope someone can help, thank you!
0
Comment
Question by:mawingho
12 Comments
 
LVL 20

Accepted Solution

by:
informaniac earned 56 total points
ID: 34923474
Store the selected values in an Arraylist and store the Arraylist object in Session. Session["selVal"] = objArrayList;
0
 
LVL 12

Assisted Solution

by:Mamtha1982
Mamtha1982 earned 55 total points
ID: 34923489
Setting sessions:

Session["sessFirstName"] = txtFirstName.Text;


Retriving sessions:
txtFirstName.Text = Session["sessFirstName"];

0
 
LVL 39

Assisted Solution

by:Pratima Pharande
Pratima Pharande earned 223 total points
ID: 34923494
Use arraylist to save values and save arraylist to session

 
To save values
// ArrayList class object
ArrayList arrlist = new ArrayList();
// add items to arrlist collection using Add method
arrlist.Add("item 1");
arrlist.Add("item 2");
arrlist.Add("item 3");
arrlist.Add("item 4");
arrlist.Add("item 5");

to save to session

Session["Company"] = arrlist;

to get values
ArrayList arrlist = new ArrayList();

arrlist = Session["Company"];
// for loop to get items stored at each index of arrlist collectionfor (int i = 0; i < arrlist.Count; i++)  {
Response.Write(arrlist[i].ToString() + "<br />");
}

Open in new window

0
 
LVL 8

Assisted Solution

by:Anurag Agarwal
Anurag Agarwal earned 55 total points
ID: 34923618
mawingho thts too simple just use session variables like ...........

// for storing any value in sessions.
Session["CompanyName"]=TxtCompanyName.text;

Using Sessions will help you to find the company name selected by the user in your first page:

//for getting the companyname selected by user in your firstpage u just need to use

Session["CompanyName"].ToString()

for eg. if you want to print the name of company selected by user in a label then do this
label1.text = Session["CompanyName"].ToString();

0
 

Assisted Solution

by:pavan_kumar_kokkiragedda
pavan_kumar_kokkiragedda earned 55 total points
ID: 34923703
Hi,

1. you can put the selected countries in a comma delimited string and put that string in a session.
     string countryselection = country1 + "," + country2.....
     session.add("countryselection", countryselection)
     
2.using a hash table for storing multiple values and place this hash table in the session
    htcountryselection.add("country1",country1)
   .....session.add("htcountry",htcountyselection)
3.using list
    list<string> lstcountryselection = new list<string>();
   lstcountryselection.add("country1");
   ......session.add("countrylist",lstcountryselection);
0
 

Author Comment

by:mawingho
ID: 34923928
Thank guys,

But which method is the best way?

Also,
How can I read each line of the GridView in order to generate the list of value?
Because use can tick the checkbox of each row...
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 39

Assisted Solution

by:Pratima Pharande
Pratima Pharande earned 223 total points
ID: 34924033
I think Arraylist is good
to get values use this

foreach (GridViewRow rowItem in GridView1.Rows){    // FindControl function gets the control placed inside the GridView control from the specified cell    // FindControl fucntion accepts string id of the control that you want to access    // type casting of control allows to access the properties of that particular control    // here checkbox control type cast is used to access its properties    chk = (CheckBox)(rowItem.Cells[0].FindControl("chk1"));        // chk.checked will access the checkbox state value (on button click event)    if (chk.Checked)    {        Response.Write( GridView1.DataKeys[rowItem.RowIndex ]["CategoryID"].ToString() + "<br />");    }}

refer
http://programming.top54u.com/post/ASP-Net-GridView-Checkbox-Value-using-C-sharp.aspx
0
 
LVL 39

Assisted Solution

by:Pratima Pharande
Pratima Pharande earned 223 total points
ID: 34924042
0
 

Assisted Solution

by:mawingho
mawingho earned 0 total points
ID: 34924076
Hi pavan_kumar_kokkiragedda,

may I know what kind of syntax is that?

List<string> lstDivisionID = new List<string>();

I never use < > to declare thing

can u explain please? Thank you!
0
 
LVL 11

Assisted Solution

by:SAMIR BHOGAYTA
SAMIR BHOGAYTA earned 56 total points
ID: 34924535
Hi, try this code

Arraylist a = new Arraylist();
a.add("1");a.add("2");a.add("2");//Now array multiple values

session["Values"] = a;
0
 
LVL 39

Assisted Solution

by:Pratima Pharande
Pratima Pharande earned 223 total points
ID: 34924573
arraylist code I have already provided to you
0
 

Author Closing Comment

by:mawingho
ID: 34977866
thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now