469,579 Members | 1,747 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,579 developers. It's quick & easy.

storing array in session variable

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?

Nov 17 '05 #1
4 31579
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,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"mechweb" <me*****@yahoo.com> wrote in message
news:uo*************@TK2MSFTNGP09.phx.gbl...
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?


Nov 17 '05 #2
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" <me*****@yahoo.com> schrieb im Newsbeitrag
news:uo*************@TK2MSFTNGP09.phx.gbl...
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?


Nov 17 '05 #3
In message <uo*************@TK2MSFTNGP09.phx.gbl>, mechweb
<me*****@yahoo.com> writes
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.

--
Steve Walker
Nov 17 '05 #4
Thanks for the quick response, that did it!

"Christof Nordiek" <cn@nospam.de> wrote in message
news:u1**************@TK2MSFTNGP14.phx.gbl...
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" <me*****@yahoo.com> schrieb im Newsbeitrag
news:uo*************@TK2MSFTNGP09.phx.gbl...
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?



Nov 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Neil Jarman | last post: by
reply views Thread by Sandra | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.