Link to home
Start Free TrialLog in
Avatar of asudhaa
asudhaa

asked on

How to save array value in session

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;
                           
            }          
        }
Avatar of radcaesar
radcaesar
Flag of India image

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>();
Avatar of asudhaa
asudhaa

ASKER

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;              
            }
         
        }
Avatar of SAMIR BHOGAYTA
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);
ASKER CERTIFIED SOLUTION
Avatar of asudhaa
asudhaa

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of asudhaa

ASKER

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.