Solved

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

Posted on 2011-02-17
12
1,505 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

828 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