Unable to store Multi-demensional array in Viewstate

R

Robert P.

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(TextWriter output,
Object value) +1117
string[,] test3 = new string[1,2];

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

tyagi_pankaj_in

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(TextWriter 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/)
 
T

tyagi_pankaj_in

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(TextWriter 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/)
 
K

Kevin Spencer

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 said:
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(TextWriter 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/)
 
M

Matt Berther

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.

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(TextWriter
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/)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top