Solved

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

Posted on 2011-02-17
12
1,378 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 39

Assisted Solution

by:Pratima Pharande
Pratima Pharande earned 223 total points
Comment Utility
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
Comment Utility
0
 

Assisted Solution

by:mawingho
mawingho earned 0 total points
Comment Utility
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
Comment Utility
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
Comment Utility
arraylist code I have already provided to you
0
 

Author Closing Comment

by:mawingho
Comment Utility
thanks
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 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

8 Experts available now in Live!

Get 1:1 Help Now