How to save array value in session

asudhaa
asudhaa used Ask the Experts™
on
I have created dynmic textboxes and I would like to save and retrieve the textbox values using session.
         I would like to save the textbox values in array .Have this in session and the retrieve this array values in another button click function.
         I used the below code.But I get the error "NullReferenceException was Unhandled by user code " when I try to save the values.

         How can I rectify this error and save the textbox values in an array?

The code is as below:

  protected void saveBtn_Click(object sender, EventArgs e)
        {
            int counter1 = 1;
            string controlid1 = string.Empty;
            ArrayList arrList = (ArrayList)Session["array"];    
   
            for (int j = 0; j < ((a)*(b - 1)); j++)
            {
                    controlid1 = "textbox" + counter1.ToString();
                    Table t = PlaceHolder1.FindControl("tablee") as Table;
                    TextBox tb = t.FindControl(controlid1) as TextBox;
                    arrList.Add(tb.Text);            
                    // Use Session
                    Session["array"] = arrList;
                    Response.Write(Session["array"]);
                    counter1 = counter1 + 1;
                           
            }          
        }
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Your error is caused by you not initializing the size of the array.

Best way to do this in .net 2.0 and beyond is to use a generic List

List<string> strtest1 = new List<string>();

Author

Commented:
Thanks,now I have used the generic list.But stil  I have only the last  strtest1[j] value in the
 Session["array"] .How can i save all the array values in the session? So that I can retrieve the session values.

protected void saveBtn_Click(object sender, EventArgs e)
        {
            int counter1 = 1;
            string controlid1 = string.Empty;
            List<string> strtest1 = new List<string>();    
   
            for (int j = 0; j < ((a)*(b - 1)); j++)
            {
                    controlid1 = "textbox" + counter1.ToString();
                    Table t = PlaceHolder1.FindControl("tableee") as Table;
                    TextBox tb = t.FindControl(controlid1) as TextBox;
                    strtest1.Add(tb.Text);            
                    // Use Session
                    Session["array"] = strtest1[j];
                    counter1 = counter1 + 1;              
            }
         
        }
Hi, this is useful to you


string[] arr1 = new string[10];

Session["myarray"] = arr1;


and other is

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 = randno;

            }
            else
            {
                i--;
            }

        }
Session["values"] = a;
In other page i used code:

int[] a = (int[])Session["values"];
Response.Write(a);
Commented:
I tried in this way and now it works for me.
string values ;
values =values + ", " + arr1[j] + "";  
Session["myarray"] = values;

and when retrieving the values I used
 string strComma = (string)Session["myarray"];
  string[] strArray = strComma.Split(',');// convert the comma seperated string of array into integer array
tb.Text = Convert.ToString(strArray[j]);

Author

Commented:
I tried to save the array values as a comma seperated string values and retrieved back.
  As I was not able to save the complete array in the session I tried this way and It worked out.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial