473,509 Members | 2,918 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Session array in c#

I am trying to create a session array then use the array in a foreach. I get
an error saying: "foreach statement cannot operate on variables of type
'object' because 'object' does not contain a definition for 'GetEnumerator',
or it is inaccessible"

The session is necessary because of postback

There are three steps here:
1. populate an array.
2. copy the array to a session array.
3. do foreach om the session array.

//step 1.-------------------------------
private void Save_Click(object sender, System.EventArgs e)
{
enrarray.Add(new
EnrollList(progid,Int32.Parse(Session["enroleeid"].ToString()),Int32.Parse(Session["sessionid"].ToString()),Int32.Parse(Session["classid"].ToString()),Int32.Parse(Session["tsid"].ToString()),Session["enrolee"].ToString(),Session["sessnm"].ToString(),Session["classnm"].ToString(),Session["tslot"].ToString()));

//step 2-----------------
Session["enr"]=enrarray;
}

//step 3-----------------------
private void Button1_Click(object sender, System.EventArgs e)
{

this.messagebox.Text="records "+enrarray.Count.ToString();

foreach(EnrollList tmp in Session["enr"])
{
tmp.saveenrl();
}
}

//error message---------------------------

c:\inetpub\wwwroot\BrighamPR\Enroll.aspx.cs(481): foreach statement cannot
operate on variables of type 'object' because 'object' does not contain a
definition for 'GetEnumerator', or it is inaccessible
Thanks in advance

NZ

Nov 22 '05 #1
3 12203
Hi NCZIMM,

Session variables are stored as objects so when you retrieve a variable
you will need to cast it to its original type before using it.

Your code doesn't appear to be telling what kind of array you are using,
so in case of a string array use this:

string[] s = (string[])Session("enr");
--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 22 '05 #2
Thanks, your help led to this solution:

ArrayList enrs = (ArrayList)Session["enr"];

I have another issue with part of the same code. the following is intended
to allow me to add multiple persons to the array each time the save buttom is
clicked (Save_Click). But, When I activate the Button1_Click (what you just
helped me with) there is only one record in the array. This is a big HUH??
for me (I am new to C# and ASP).

private void Save_Click(object sender, System.EventArgs e)
{
enrarray.Add(new
EnrollList(progid,Int32.Parse(Session["enroleeid"].ToString()),Int32.Parse(Session["sessionid"].ToString()),Int32.Parse(Session["classid"].ToString()),Int32.Parse(Session["tsid"].ToString()),Session["enrolee"].ToString(),Session["sessnm"].ToString(),Session["classnm"].ToString(),Session["tslot"].ToString()));
Session["enr"]=enrarray;
}

"Morten Wennevik" wrote:
Hi NCZIMM,

Session variables are stored as objects so when you retrieve a variable
you will need to cast it to its original type before using it.

Your code doesn't appear to be telling what kind of array you are using,
so in case of a string array use this:

string[] s = (string[])Session("enr");
--
Happy Coding!
Morten Wennevik [C# MVP]

Nov 22 '05 #3
NCZIMM,

Try retrieving the stored arraylist before adding the new item

private void Save_Click(object sender, System.EventArgs e)
{

enrarray = (ArrayList)Session["enr"];
enrarray.Add(new
EnrollList(progid,Int32.Parse(Session["enroleeid"].ToString()),Int32.Parse(Session["sessionid"].ToString()),Int32.Parse(Session["classid"].ToString()),Int32.Parse(Session["tsid"].ToString()),Session["enrolee"].ToString(),Session["sessnm"].ToString(),Session["classnm"].ToString(),Session["tslot"].ToString()));

Session["enr"]=enrarray;
}
On Thu, 4 Nov 2004 12:55:03 -0800, NCZIMM
<NC****@discussions.microsoft.com> wrote:
Thanks, your help led to this solution:

ArrayList enrs = (ArrayList)Session["enr"];

I have another issue with part of the same code. the following is
intended
to allow me to add multiple persons to the array each time the save
buttom is
clicked (Save_Click). But, When I activate the Button1_Click (what you
just
helped me with) there is only one record in the array. This is a big
HUH??
for me (I am new to C# and ASP).

private void Save_Click(object sender, System.EventArgs e)
{
enrarray.Add(new
EnrollList(progid,Int32.Parse(Session["enroleeid"].ToString()),Int32.Parse(Session["sessionid"].ToString()),Int32.Parse(Session["classid"].ToString()),Int32.Parse(Session["tsid"].ToString()),Session["enrolee"].ToString(),Session["sessnm"].ToString(),Session["classnm"].ToString(),Session["tslot"].ToString()));
Session["enr"]=enrarray;
}

"Morten Wennevik" wrote:
Hi NCZIMM,

Session variables are stored as objects so when you retrieve a variable
you will need to cast it to its original type before using it.

Your code doesn't appear to be telling what kind of array you are using,
so in case of a string array use this:

string[] s = (string[])Session("enr");
--
Happy Coding!
Morten Wennevik [C# MVP]


--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
6773
by: Bart Plessers \(artabel\) | last post by:
Hello, My script builds a list of files in a folder. These files are stored in an 2-dimensional array, called "a_files". To speed up the script, I don't want the filelist everytime being read...
3
2998
by: NCZIMM | last post by:
I am trying to create a session array then use the array in a foreach. I get an error saying: "foreach statement cannot operate on variables of type 'object' because 'object' does not contain a...
9
3647
by: bajopalabra | last post by:
hi session("myVar") = rs.getRows( ) don't work when number of records is greater than 10 does anybody know WHY ??? is it a Session object limitation ??? thanks
2
1687
by: adam | last post by:
Having spent nearly 2 years in win forms land the inevitable request came for me to "do some web pages". So being new to this bit of .net and having had a look around I can't see where the best...
9
9760
by: charliewest | last post by:
Hello - I have images saved in my SQL SERVER 2000 database. Using ASP.NET (C#) is there any way to temporarily save an image to a session object, and after running some other operations, later...
6
3757
by: Bahman | last post by:
Hello! I have a simple question. Do we have session arrays that we can reference, assign, or select from? Could I please have a sample of how this is done. The obvious syntax that I am...
3
2327
by: Brad | last post by:
I am storing an array which contains about a dozen chracter items to a Session variable. Later, I need to use this array so I am doing the following: Dim eventTypes As String() =...
5
3859
by: Diffident | last post by:
Hello All, I have a 2-dimensional array that I am storing as a session variable. I have no idea on how I can cast the session variable back to 2-dimensional array. Any pointers? Reference...
5
3162
by: TRB_NV | last post by:
I'm losing information from my Session when I change pages or start the same page over again. I simplified the code so the example is really clear. The sample code that follows is supposed to...
4
2321
by: Vince13 via DotNetMonster.com | last post by:
I am trying to set up a page where the user can change data, and then click cancel and the data will not be saved. Currently, I am saving the data in a Session variable that is an array of a class...
0
7136
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7412
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7069
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5652
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4730
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3216
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1570
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
441
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.