Session array in c#

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
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");
 
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;
}
 
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;
}


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 said:
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");
 
Back
Top