10 Replies - 17128 Views - Last Post: 23 April 2010 - 10:47 AM Rate Topic: -----

#1 Guest_arron*


Reputation:

storing ArrayList in session data

Posted 23 April 2010 - 07:01 AM

Hi there, I have slowly been making progress towards storing an arraylist in session data so that when I sum all the elements of an array, the sum will change because a new random number has been added to the arraylist upon a button click. However I am having a problem iterating through the arraylist....

protected void twist2(object sender, EventArgs e)
    {
        int a = 11;
        int c = RandomNumber(a);
        score.Add(c);
        score = (ArrayList)Session["scoreData"];
        
        Result.Text = (c).ToString();
        String Result_id = Result.Text;

        int total = 0;
        Session["scoreData"] = score;
        for (int i = 0; i < score.Count; ++i)
        {
            total += (int)score[i];
        }
        Sum.Text = (total).ToString();
        String Sum_id = Sum.Text;

    }


and I get the following error message concerning score.Count; :

NullReferenceException unhandled by the user
Object reference not set to an instance of an object.


It seems I am having trouble generating the arraylist in the session, but don't know how to solve this....

Is This A Good Question/Topic? 0

Replies To: storing ArrayList in session data

#2 Guest_arron*


Reputation:

Re: storing ArrayList in session data

Posted 23 April 2010 - 07:06 AM

oh by the way, ArrayList score is defined globally
Was This Post Helpful? 0

#3 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: storing ArrayList in session data

Posted 23 April 2010 - 07:44 AM

Did you instantiate the ArrayList with the New keyword?

Where, besides the twist2 method, are you putting the ArrayList in the session variable?
Was This Post Helpful? 0
  • +
  • -

#4 Guest_arron*


Reputation:

Re: storing ArrayList in session data

Posted 23 April 2010 - 08:01 AM

I have instansiated the ArrayList with new. It is defined outside the twist method:

public partial class _Default : System.Web.UI.Page {

  ArrayList score = new ArrayList();
    

    protected void twist2(object sender, EventArgs e)
    {
        int a = 11;
        int c = RandomNumber(a);
        score.Add(c);
        score = (ArrayList)Session["scoreData"];

        Result.Text = (c).ToString();
        String Result_id = Result.Text;

        int total = 0;
        //Session["scoreData"] = score;
        
        for (int i = 0; i < score.Count; ++i)
        {
            total += (int)score[i];
        }
        Sum.Text = (total).ToString();
        String Sum_id = Sum.Text;

    }


as you mentioned, I must not be storing the arraylist in the session variable. Even though to me it looks like it should be getting stored. I hope the issue a bit clearer now!

Thanks for the reply
Was This Post Helpful? 0

#5 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: storing ArrayList in session data

Posted 23 April 2010 - 09:11 AM

In your Page_Load event store the ArrayList in your session. The issue appears that you never save it to session before you try to use it. Check if this is a PostBack to prevent it from overwriting the value stored in the session. You only want to do this once in the Load event when the page first loads.

See if that solves the issue for you.

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
            Session["scoreData"] = score;
    }

Was This Post Helpful? 0
  • +
  • -

#6 Guest_Guest*


Reputation:

Re: storing ArrayList in session data

Posted 23 April 2010 - 09:28 AM

Yes I see what you are saying. I have tried this and c still doesn't get added to the arraylist. I had even tried adding:

if(score == null){

score = new ArrayList():

}

but that hasn't worked either.
Was This Post Helpful? 0

#7 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: storing ArrayList in session data

Posted 23 April 2010 - 09:34 AM

Ok lets try a different tact. Post your code behind in its entirety.

Lets see what you are doing elsewhere, perhaps it is having an impact on your session.
Was This Post Helpful? 0
  • +
  • -

#8 Guest_arron*


Reputation:

Re: storing ArrayList in session data

Posted 23 April 2010 - 09:37 AM

Here is the entire code:

public partial class _Default : System.Web.UI.Page 
{
    
    string _connectionString = ConfigurationManager.ConnectionStrings["numbersConnectionString"].ToString();
    protected void Page_Load(object sender, EventArgs e)
    {
     
        if (!Page.IsPostBack)
        {
            Session["scoreData"] = score;
        }
    }
    private int RandomNumber(int max)
    {
        Random random = new Random();
        return random.Next(max);
    }
    ArrayList score = new ArrayList();
    protected void twist2(object sender, EventArgs e)
    {
        int a = 11;
        int c = RandomNumber(a);
        score.Add(c);
        score = (ArrayList)Session["scoreData"];
        Result.Text = (c).ToString();
        String Result_id = Result.Text;

        int total = 0;
        for (int i = 0; i < score.Count; ++i)
        {
            total += (int)score[i];
        }
        Sum.Text = (total).ToString();
        String Sum_id = Sum.Text;
        
        SqlConnection myConnection = new SqlConnection(_connectionString);
        myConnection.Open();
        try
        {
            SqlCommand myCommand = new SqlCommand("INSERT INTO cards " +
                                         "Values ('" + Result_id + "')", myConnection);


            myCommand.ExecuteNonQuery();
            

        }
        catch (Exception ex)
        {    
        }
       
        myConnection.Close();
        GridView1.DataBind();
        Repeater1.DataBind();
    }

    protected void Arg1_TextChanged(object sender, EventArgs e)
    {

    }
    protected void Result_TextChanged(object sender, EventArgs e)
    {

    }
    protected void Sum_TextChanged(object sender, EventArgs e)
    {
        
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection myConnection = new SqlConnection(_connectionString);
        myConnection.Open();
        SqlCommand myCommand = new SqlCommand(" DELETE FROM cards ", myConnection);
        myCommand.ExecuteNonQuery();
        myConnection.Close();
        GridView1.DataBind();

    }
    protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
    {

    }
    protected void GridView1_SelectedIndexChanged1(object sender, EventArgs e)
    {

    }
}


It's a bit messy I know but it all compiles fine.
Was This Post Helpful? 0

#9 keakTheGEEK  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 107
  • View blog
  • Posts: 344
  • Joined: 23-February 10

Re: storing ArrayList in session data

Posted 23 April 2010 - 10:18 AM

In your code snippet below
 int a = 11;
        int c = RandomNumber(a);
        score.Add(c);
        score = (ArrayList)Session["scoreData"];
        Result.Text = (c).ToString();
        String Result_id = Result.Text;




The problem is here:
score.Add(c);
score = (ArrayList)Session["scoreData"];



You add a random number to score, then you set score equal to the arraylist stored in your session, essentially over writing what you just did. You need to reverse those two lines. First, set score equal to the arraylist stored in score, then add c to it, then before that function ends you need to store it back into the session...
Was This Post Helpful? 0
  • +
  • -

#10 Guest_arron*


Reputation:

Re: storing ArrayList in session data

Posted 23 April 2010 - 10:45 AM

Yes! That was indeed the problem! The code works fine now, so I can now move on to the next part.

Thank you to the both of you!
Was This Post Helpful? 0

#11 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: storing ArrayList in session data

Posted 23 April 2010 - 10:47 AM

Depending on how large your ArrayList gets it could start degrading your performance and that's something you dont want to happen. I would take a look at caching instead of storing it in the session
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1