Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2011-02-17
12
Medium Priority
?
1,797 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
12 Comments
 
LVL 20

Accepted Solution

by:
informaniac earned 224 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:Praveen Kumar
Praveen Kumar earned 220 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 892 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 8

Assisted Solution

by:Anurag Agarwal
Anurag Agarwal earned 220 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 220 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 892 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 892 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 224 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 892 total points
ID: 34924573
arraylist code I have already provided to you
0
 

Author Closing Comment

by:mawingho
ID: 34977866
thanks
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

721 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