Connecting Tech Pros Worldwide Help | Site Map

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

Newbie
 
Join Date: May 2007
Posts: 28
#1: Oct 5 '07
hi
i'm using the following statement in my application
string[] strTArray = new string[5];
strTArray = (string[])(Session["TextDataArray"]);

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's Avatar
Moderator
 
Join Date: Aug 2007
Location: Brisbane, Australia
Posts: 1,414
#2: Oct 5 '07

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


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

cheers.
Newbie
 
Join Date: May 2007
Posts: 28
#3: Oct 5 '07

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


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


Quote:

Originally Posted by Shashi Sadasivan

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

cheers.

Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,148
#4: Oct 5 '07

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


yes.
store your data as a single string.
since textboxes keep their data as a single string it should be fairly simple to do.
Newbie
 
Join Date: Oct 2007
Posts: 4
#5: Oct 5 '07

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


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>(arrayinput);

//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.
Reply