"Unable to cast object of type 'System.String' to type 'System.String[]'."

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • archu007
    New Member
    • May 2007
    • 28

    "Unable to cast object of type 'System.String' to type 'System.String[]'."

    hi
    i'm using the following statement in my application
    string[] strTArray = new string[5];
    strTArray = (string[])(Session["TextDataAr ray"]);

    when i run the application its giving the below error

    {"Unable to cast object of type 'System.String' to type 'System.String[]'."}


    can anyone say how to solve this
    thanks in advance
    Archu
  • Shashi Sadasivan
    Recognized Expert Top Contributor
    • Aug 2007
    • 1435

    #2
    That session variable does not store a string array.
    that is what .net means by throwing that error.

    cheers.

    Comment

    • archu007
      New Member
      • May 2007
      • 28

      #3
      hi
      but i want to use that as i'm creating dynamic controls i want to store their values
      is there any other way to do that


      Originally posted by Shashi Sadasivan
      That session variable does not store a string array.
      that is what .net means by throwing that error.

      cheers.

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        yes.
        store your data as a single string.
        since textboxes keep their data as a single string it should be fairly simple to do.

        Comment

        • numena79
          New Member
          • Oct 2007
          • 4

          #5
          Why don't you try using an Arraylist (or more specifically a Generic List) for your input instead?

          If you have not choice but to store your input in a string[] , see if this works,

          //this would be your original input array
          string[] arrayinput = new string[] { "hello", "world" };

          //convert it to a generic list of strings
          List<string> test = new List<string>(ar rayinput);

          //store the above list in session
          Session["test"] = test;

          //retrieve the list from the session into an string[] array
          string[] testarray = ((List<string>) Session["test"]).ToArray();

          Kind of circuitous, but it would get the job done.

          Thanks.

          Comment

          Working...