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 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]
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]
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] This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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...
|
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
|
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...
|
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...
| |
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...
|
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() =...
|
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...
|
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...
|
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...
|
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...
| |
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...
|
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...
|
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,...
|
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...
|
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...
|
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 ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |