Click here to Skip to main content
15,936,903 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to store two dimensional array in Session that is in int originaly and then I need to retrieve from the Session. Is some way? I can not solve that for so long. It does not work and it says:
System.InvalidCastException: Unable to cast object of type 'System.Web.UI.WebControls.Label' to type 'System.Int32[,]'.


What I have tried:

I have tried everything but I can not figure that out.


Random rnd = new Random();
      int r = Convert.ToInt32(pocetR.Text);
      int s = Convert.ToInt32(pocetS.Text);
      int[,] MaticeA = new int[r, s];

      int[,] MaticeB = new int[r, s];

      for (int j = 0; j < s; j++)
      {
          for (int i = 0; i < r; i++)
          {
              int ko = rnd.Next(-50, 50);

              MaticeA[i, j] = ko;

              maticeA.Text = maticeA.Text + MaticeA[i ,j].ToString();
              maticeA.Text = maticeA.Text + " ";
              Session["MA"] = Session["MA"] + ' '.ToString();
          }
          maticeA.Text = maticeA.Text + "<br />";
          Session["MyArray"] = maticeA;
      }

      for (int j = 0; j < s; j++)
      {
          for (int i = 0; i < r; i++)
          {
              MaticeB[i, j] = rnd.Next(-50, 50);
              maticeB.Text = maticeB.Text + MaticeB[i, j].ToString();
              maticeB.Text = maticeB.Text + " ";
          }
          maticeB.Text = maticeB.Text + "<br />";
      }


  }

  protected void vypocitat_Click(object sender, EventArgs e)
  {

      int[,] fromSession = (int[,])Session["MyArray"];


      lblV.Text = fromSession[0, 1].ToString();




  }
Posted
Updated 22-May-20 2:20am
v4

C#
// Create
int[,] a = new int[2, 2];
a[0, 0] = 1;
a[0, 1] = 2;
a[1, 0] = 3;
a[1, 1] = 4;

// Store
Session["MyArray"] = a;

// Retrieve
int[,] fromSession = (int[,])Session["MyArray"];
 
Share this answer
 
Comments
Petr Jaroš 22-May-20 8:17am    
It does not work it says: System.InvalidCastException: Unable to cast object of type 'System.Web.UI.WebControls.Label' to type 'System.Int32[,]'.

I need to store that because after clicking on the button the data will be disappeared.
Richard MacCutchan 22-May-20 8:19am    
If your data is not a set of integers then you obviously don't declare the array as int type.
F-ES Sitecore 22-May-20 8:22am    
Reading between the lines your data is the text of a label, but you'll need to parse that label and turn it into an array before storing it, .net can't do that for you.
F-ES Sitecore 22-May-20 8:30am    
c# is case-sensitive, you have maticeA and MaticeA which is a bad idea. MaticeA is your array so that is what you need to store

Session["MyArray"] = MaticeA;
Petr Jaroš 22-May-20 8:30am    
Thanks. I got that.
You could try to store it as an unidimensional array instead.
C#
readonly int R = 3;
readonly int C = 4;
int[,] bidim = new int[R, C];
int[] unidim = new int[R * C];

// bidim to unidim
for (int c = 0; c < C; ++c)
{
   for (int r = 0; r < R; ++r)
   {
      unidim[r * C + c] = bidim[r, c];
   }
}

// unidim to bidim
for (int c = 0; c < C; ++c)
{
   for (int r = 0; r < R; ++r)
   {
      bidim[r, c] = unidim[r * C + c];
   }
}
 
Share this answer
 
If you're using "traditional" ASP.NET, you can just store the value directly in the session:
C#
int[,] values = { { 1, 2 }, { 42, 64 } };
Session["SomeUniqueKey"] = value;
int[,] otherValues = (int[,])Session["SomeUniqueKey"];


If you're using ASP.NET Core, things are slightly more complicated. You can directly store a byte array, and there are extension methods for strings and single integers. But there's nothing for arrays of other types, or more complicated objects.
Session in ASP.NET Core | Microsoft Docs[^]
ISession Interface (Microsoft.AspNetCore.Http) | Microsoft Docs[^]

The usual approach seems to be to serialize the value to JSON and store it as a string:
C#
int[,] values = { { 1, 2 }, { 42, 64 } };
Session.SetString("SomeUniqueKey", JsonConvert.SerializeObject(value));
int[,] otherValues = JsonConvert.DeserializeObject<int[,]>(Session.GetString("SomeUniqueKey"));
However, there may be more efficient ways to accomplish this.
 
Share this answer
 
v2
Comments
Petr Jaroš 22-May-20 8:30am    
Thank you so much it helped.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900