Solved

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

Posted on 2011-02-17
12
1,451 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

813 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

10 Experts available now in Live!

Get 1:1 Help Now