Ishika Ishika - 1 month ago 5
ASP.NET (C#) Question

retrieving values in array & store in session and calling to other page asp.net c#

Hi
I want to store values inside the array in session. I want to use it to another page. How i can do that?
I used session to store values of array as follow:

int i, randno;
int[] a = new int[5];
for ( i = 0; i < 4; i++)
{
int flag = 0;
Random rnd = new Random();
randno = rnd.Next(1, 15);
for (int j = 0; j < i; j++)
{
if (a[j] == randno)
flag = 1;
}
if (flag == 0)
{
a[i] = randno;

}
else
{
i--;
}

}
Session["values"] = a;


In other page i used code:

int[] a = (int[])Session["values"];
Response.Write(a);


This code is right?
Because its not giving values.
But retrieving to another page then it gives last value of the array, on the same page it gives all the values. I want all the values of the array.
Asp.net, c#
Thank you.

Answer

Try this

To store all items of the array.

string[] a = new string[]{"a","b","c"};
Session["values"] = a;

And in the next page, you can retrieve it like this.

string[] a = (string[])Session["values"]