storing array in session variable

  • Thread starter Thread starter mechweb
  • Start date Start date
M

mechweb

Hi all, I can successfully store an array in a session variable like so:
string[] arr1 = new string[10];

Session["myarray"] = arr1;

But, when I try to display the elements (using the code below) on another
page I get this error:

txtmyarray.Text = Session["myarray"][0];

Cannot apply indexing with [] to an expression of type 'object'

The weird thing is that the code works fine in the immediate window while
debugging. Any thoughts?
 
hi,

My advise is that you should read an introductory book to c# , this is
based on both your questions.

This will solve your problem:
txtmyarrat.Text = ((string[])Session["myarray"])[0];

cheers,
 
Hi,

you've got to cast it back to an array type.

txtmyarray.Text = (string)((Array)Session["myarray"])[0]
or
txtmyarray.Text = ((string[])Session["myarray"])[0]

Christof
 
mechweb said:
Hi all, I can successfully store an array in a session variable like so:
string[] arr1 = new string[10];

Session["myarray"] = arr1;

But, when I try to display the elements (using the code below) on another
page I get this error:

txtmyarray.Text = Session["myarray"][0];

Cannot apply indexing with [] to an expression of type 'object'

The weird thing is that the code works fine in the immediate window while
debugging. Any thoughts?

Session["myarray"] is returning type object. Cast it to string[] before
trying to access it as such.
 
Thanks for the quick response, that did it!

Christof Nordiek said:
Hi,

you've got to cast it back to an array type.

txtmyarray.Text = (string)((Array)Session["myarray"])[0]
or
txtmyarray.Text = ((string[])Session["myarray"])[0]

Christof

mechweb said:
Hi all, I can successfully store an array in a session variable like so:
string[] arr1 = new string[10];

Session["myarray"] = arr1;

But, when I try to display the elements (using the code below) on another
page I get this error:

txtmyarray.Text = Session["myarray"][0];

Cannot apply indexing with [] to an expression of type 'object'

The weird thing is that the code works fine in the immediate window while
debugging. Any thoughts?
 
Back
Top