storing array in session variable

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mechweb

    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?





  • Ignacio Machin \( .NET/ C# MVP \)

    #2
    Re: storing array in session variable

    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" <mechweb@yahoo. com> wrote in message
    news:uoXYUjncFH A.456@TK2MSFTNG P09.phx.gbl...[color=blue]
    > 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?
    >
    >
    >
    >
    >[/color]


    Comment

    • Christof Nordiek

      #3
      Re: storing array in session variable

      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" <mechweb@yahoo. com> schrieb im Newsbeitrag
      news:uoXYUjncFH A.456@TK2MSFTNG P09.phx.gbl...[color=blue]
      > 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?
      >
      >
      >
      >
      >[/color]


      Comment

      • Steve Walker

        #4
        Re: storing array in session variable

        In message <uoXYUjncFHA.45 6@TK2MSFTNGP09. phx.gbl>, mechweb
        <mechweb@yahoo. com> writes[color=blue]
        >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.Tex t = 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?[/color]

        Session["myarray"] is returning type object. Cast it to string[] before
        trying to access it as such.

        --
        Steve Walker

        Comment

        • mechweb

          #5
          Re: storing array in session variable

          Thanks for the quick response, that did it!

          "Christof Nordiek" <cn@nospam.de > wrote in message
          news:u14oqpncFH A.4040@TK2MSFTN GP14.phx.gbl...[color=blue]
          > 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" <mechweb@yahoo. com> schrieb im Newsbeitrag
          > news:uoXYUjncFH A.456@TK2MSFTNG P09.phx.gbl...[color=green]
          >> 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?
          >>
          >>
          >>
          >>
          >>[/color]
          >
          >[/color]


          Comment

          Working...