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(obje ct sender, System.EventArg s e)
{
enrarray.Add(ne w
EnrollList(prog id,Int32.Parse( Session["enroleeid"].ToString()),In t32.Parse(Sessi on["sessionid"].ToString()),In t32.Parse(Sessi on["classid"].ToString()),In t32.Parse(Sessi on["tsid"].ToString()),Se ssion["enrolee"].ToString(),Ses sion["sessnm"].ToString(),Ses sion["classnm"].ToString(),Ses sion["tslot"].ToString()));
//step 2-----------------
Session["enr"]=enrarray;
}
//step 3-----------------------
private void Button1_Click(o bject sender, System.EventArg s e)
{
this.messagebox .Text="records "+enrarray.Coun t.ToString();
foreach(EnrollL ist tmp in Session["enr"])
{
tmp.saveenrl();
}
}
//error message---------------------------
c:\inetpub\wwwr oot\BrighamPR\E nroll.aspx.cs(4 81): 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 12222
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)Sess ion["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(obje ct sender, System.EventArg s e)
{
enrarray.Add(ne w
EnrollList(prog id,Int32.Parse( Session["enroleeid"].ToString()),In t32.Parse(Sessi on["sessionid"].ToString()),In t32.Parse(Sessi on["classid"].ToString()),In t32.Parse(Sessi on["tsid"].ToString()),Se ssion["enrolee"].ToString(),Ses sion["sessnm"].ToString(),Ses sion["classnm"].ToString(),Ses sion["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(obje ct sender, System.EventArg s e)
{
enrarray = (ArrayList)Sess ion["enr"];
enrarray.Add(ne w
EnrollList(prog id,Int32.Parse( Session["enroleeid"].ToString()),In t32.Parse(Sessi on["sessionid"].ToString()),In t32.Parse(Sessi on["classid"].ToString()),In t32.Parse(Sessi on["tsid"].ToString()),Se ssion["enrolee"].ToString(),Ses sion["sessnm"].ToString(),Ses sion["classnm"].ToString(),Ses sion["tslot"].ToString()));
Session["enr"]=enrarray;
}
On Thu, 4 Nov 2004 12:55:03 -0800, NCZIMM
<NC****@discuss ions.microsoft. com> wrote: Thanks, your help led to this solution:
ArrayList enrs = (ArrayList)Sess ion["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(obje ct sender, System.EventArg s e) { enrarray.Add(ne w EnrollList(prog id,Int32.Parse( Session["enroleeid"].ToString()),In t32.Parse(Sessi on["sessionid"].ToString()),In t32.Parse(Sessi on["classid"].ToString()),In t32.Parse(Sessi on["tsid"].ToString()),Se ssion["enrolee"].ToString(),Ses sion["sessnm"].ToString(),Ses sion["classnm"].ToString(),Ses sion["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 on
the server.
Therefore I wanted to send the contents of a_files to a session variable.
However, with
|
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 definition for 'GetEnumerator',
or it is inaccessible"
The session is necessary because of postback
There are three steps here:
1. populate an...
|
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 way to store session data.
1) use the (string)Session approach
Obviously bad for maintenance, readability etc.
2) have a typed MySession object...
|
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 retrieve the image from the session
object, convert it back to an image, and re-save it to the database?
Thanks?
| |
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 trying doesn't work. There are SA's in VB, but I am not seeing anything
for C#.
|
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() = DirectCast(Session("EventTypes"), String())
If Date.Today <= closeDate Then
If eventTypes(cblEntries.SelectedIndex) = "J" Then thisFee =
Session("JRFee") Else...
|
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 code below...
Array declaration:
DateTime DateRangesForDataLists = new DateTime;
|
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 generate a random number and put it
into an Array and store it in the Session variable and then when it runs the
next time, generate another random...
|
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 I created:
public class cut
{
public cut()
{
ParallelDim = 'G';
cutPoint = ++numCuts;
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
| |
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |