By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,701 Members | 1,970 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,701 IT Pros & Developers. It's quick & easy.

Unable to store Multi-demensional array in Viewstate

P: n/a
I can easily store a one-dimensional array in viewstate ( see Test1 )

If I try storing a multi-dimensional array in the viewstate it's crapping
out on me when it goes to [de]serialize the array (not when I make the
assignment).

Interestingly, I get a very different error depending on the type of array
( string or decimal ).

I'm not doing anything fancy, just a regular old aspx page with a single
button and the following code ( commented/uncomment as desired):

// TEST #1 -- This works
string[] test1 = new string[1];
test1[0] = "0";
ViewState["_test1"] = test1;
// TEST #2 -- Causes Error
// ERROR: Array was not a one-dimensional array.
// TRACE: System.Array.GetValue(Int32 index) +80
decimal[,] test2 = new decimal[1,2];

test2[0,0] = (decimal)1.0;
test2[0,1] = (decimal)2.2;
ViewState["_test2"] = test2;
// TEST #3 -- Causes Error
// ERROR: Specified cast is not valid.
// TRACE: System.Web.UI.LosFormatter.SerializeValue(TextWrit er output,
Object value) +1117
string[,] test3 = new string[1,2];

test3[0,0] = "00";
test3[0,1] = "01";
ViewState["_test3"] = test3;
Nov 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a

I can easily store a one-dimensional array in viewstate ( see Test1 )

If I try storing a multi-dimensional array in the viewstate it's crapping
out on me when it goes to [de]serialize the array (not when I make the
assignment).

Interestingly, I get a very different error depending on the type of array
( string or decimal ).

I'm not doing anything fancy, just a regular old aspx page with a single
button and the following code ( commented/uncomment as desired):

// TEST #1 -- This works
string[] test1 = new string[1];
test1[0] = "0";
ViewState["_test1"] = test1;
// TEST #2 -- Causes Error
// ERROR: Array was not a one-dimensional array.
// TRACE: System.Array.GetValue(Int32 index) +80
decimal[,] test2 = new decimal[1,2];

test2[0,0] = (decimal)1.0;
test2[0,1] = (decimal)2.2;
ViewState["_test2"] = test2;
// TEST #3 -- Causes Error
// ERROR: Specified cast is not valid.
// TRACE: System.Web.UI.LosFormatter.SerializeValue(TextWrit er output,
Object value) +1117
string[,] test3 = new string[1,2];

test3[0,0] = "00";
test3[0,1] = "01";
ViewState["_test3"] = test3;


User submitted from AEWNET (http://www.aewnet.com/)
Nov 18 '05 #2

P: n/a
Hi All,

I also have same problem. I want to store 2 dimensional array object into viewstate
but getting same problem
With Regards,
Pankaj Tyagi

I can easily store a one-dimensional array in viewstate ( see Test1 )

If I try storing a multi-dimensional array in the viewstate it's crapping
out on me when it goes to [de]serialize the array (not when I make the
assignment).

Interestingly, I get a very different error depending on the type of array
( string or decimal ).

I'm not doing anything fancy, just a regular old aspx page with a single
button and the following code ( commented/uncomment as desired):

// TEST #1 -- This works
string[] test1 = new string[1];
test1[0] = "0";
ViewState["_test1"] = test1;
// TEST #2 -- Causes Error
// ERROR: Array was not a one-dimensional array.
// TRACE: System.Array.GetValue(Int32 index) +80
decimal[,] test2 = new decimal[1,2];

test2[0,0] = (decimal)1.0;
test2[0,1] = (decimal)2.2;
ViewState["_test2"] = test2;
// TEST #3 -- Causes Error
// ERROR: Specified cast is not valid.
// TRACE: System.Web.UI.LosFormatter.SerializeValue(TextWrit er output,
Object value) +1117
string[,] test3 = new string[1,2];

test3[0,0] = "00";
test3[0,1] = "01";
ViewState["_test3"] = test3;


User submitted from AEWNET (http://www.aewnet.com/)
Nov 18 '05 #3

P: n/a
Multi-dimensional arrays are not serializable. I am not sure how this would
work out, but you might try using a one-dimensional array of one-dimensional
arrays. This is roughly equivalent to a two-dimensional array, but is known
as a "jagged array" because the length of the arrays that are in the first
array are not necessarily the same.

As I've never tried this, I can't guarantee that it would work.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
I get paid good money to
solve puzzles for a living

"tyagi_pankaj_in" <tyagi_pankaj_in@aew_nospam.com> wrote in message
news:ex**************@TK2MSFTNGP14.phx.gbl...
Hi All,

I also have same problem. I want to store 2 dimensional array object into viewstate but getting same problem
With Regards,
Pankaj Tyagi

I can easily store a one-dimensional array in viewstate ( see Test1 )

If I try storing a multi-dimensional array in the viewstate it's crapping out on me when it goes to [de]serialize the array (not when I make the
assignment).

Interestingly, I get a very different error depending on the type of array ( string or decimal ).

I'm not doing anything fancy, just a regular old aspx page with a single
button and the following code ( commented/uncomment as desired):

// TEST #1 -- This works
string[] test1 = new string[1];
test1[0] = "0";
ViewState["_test1"] = test1;
// TEST #2 -- Causes Error
// ERROR: Array was not a one-dimensional array.
// TRACE: System.Array.GetValue(Int32 index) +80
decimal[,] test2 = new decimal[1,2];

test2[0,0] = (decimal)1.0;
test2[0,1] = (decimal)2.2;
ViewState["_test2"] = test2;
// TEST #3 -- Causes Error
// ERROR: Specified cast is not valid.
// TRACE: System.Web.UI.LosFormatter.SerializeValue(TextWrit er output,
Object value) +1117
string[,] test3 = new string[1,2];

test3[0,0] = "00";
test3[0,1] = "01";
ViewState["_test3"] = test3;


User submitted from AEWNET (http://www.aewnet.com/)

Nov 18 '05 #4

P: n/a
Hello Kevin,

In addition, a multi-dimensional array is typically the sign of a "code smell[1]".
A better route to go would be to use a class that has properties for each
portion of the multi-dimensional array. After that, use either a collection
or an array to save it.

[1] http://c2.com/cgi/wiki?WhatIsaSmell

--
Matt Berther
http://www.mattberther.com
Multi-dimensional arrays are not serializable. I am not sure how this
would work out, but you might try using a one-dimensional array of
one-dimensional arrays. This is roughly equivalent to a
two-dimensional array, but is known as a "jagged array" because the
length of the arrays that are in the first array are not necessarily
the same.

As I've never tried this, I can't guarantee that it would work.

"tyagi_pankaj_in" <tyagi_pankaj_in@aew_nospam.com> wrote in message
news:ex**************@TK2MSFTNGP14.phx.gbl...
Hi All,

I also have same problem. I want to store 2 dimensional array object
into

viewstate
but getting same problem

With Regards,
Pankaj Tyagi
I can easily store a one-dimensional array in viewstate ( see Test1
)

If I try storing a multi-dimensional array in the viewstate it's
crapping
out on me when it goes to [de]serialize the array (not when I make
the assignment).

Interestingly, I get a very different error depending on the type of
array
( string or decimal ).

I'm not doing anything fancy, just a regular old aspx page with a
single button and the following code ( commented/uncomment as
desired):

// TEST #1 -- This works
string[] test1 = new string[1];
test1[0] = "0";
ViewState["_test1"] = test1;
// TEST #2 -- Causes Error
// ERROR: Array was not a one-dimensional array.
// TRACE: System.Array.GetValue(Int32 index) +80
decimal[,] test2 = new decimal[1,2];
test2[0,0] = (decimal)1.0;
test2[0,1] = (decimal)2.2;
ViewState["_test2"] = test2;
// TEST #3 -- Causes Error
// ERROR: Specified cast is not valid.
// TRACE: System.Web.UI.LosFormatter.SerializeValue(TextWrit er
output,
Object value) +1117
string[,] test3 = new string[1,2];
test3[0,0] = "00";
test3[0,1] = "01";
ViewState["_test3"] = test3;

User submitted from AEWNET (http://www.aewnet.com/)

Nov 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.