2 Replies - 1033 Views - Last Post: 10 January 2016 - 07:56 PM

#1 lboyce72  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 18-August 15

Save textbox to array in session using C# code behind

Posted 27 December 2015 - 03:25 PM

Not sure if this is the correct section for this question... Up front, I am in school and I have included my code below. I have a simple calculator that the user clicks "Store Data" to save the result in an array. With my current code, I can save the result to a textbox, but with a second result "Store Data" click, the first result is erased and replaced with the second result. I believe the issue is with the session state. Appreciate any guidance you can provide.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.SessionState;


public partial class _Default : System.Web.UI.Page
{
    double dblNum1, dblNum2;

    string[] arrStore = {};
    
    protected void Page_Load(object sender, EventArgs e)
    {

        if (Session["Store"] == null)
        {
            Session["Store"] = arrStore;
        }

        else
        {
            string[] arrStore = (string[])Session["Store"];
        }
    }


//Add 2 numbers
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        dblNum1 = Convert.ToDouble(txtNum1.Text);
        dblNum2 = Convert.ToDouble(txtNum2.Text);
        txtResult.Text = Convert.ToString(dblNum1 + dblNum2);
    }

//Subtract 2 numbers
    protected void btnSubtract_Click(object sender, EventArgs e)
    {
        dblNum1 = Convert.ToDouble(txtNum1.Text);
        dblNum2 = Convert.ToDouble(txtNum2.Text);
        txtResult.Text = Convert.ToString(dblNum1 - dblNum2);
    }

//Multiply 2 numbers
    protected void btnMultiply_Click(object sender, EventArgs e)
    {
        dblNum1 = Convert.ToDouble(txtNum1.Text);
        dblNum2 = Convert.ToDouble(txtNum2.Text);
        txtResult.Text = Convert.ToString(dblNum1 * dblNum2);
    }

//Divide 2 numbers
    protected void btnDivide_Click(object sender, EventArgs e)
    {
        dblNum1 = Convert.ToDouble(txtNum1.Text);
        dblNum2 = Convert.ToDouble(txtNum2.Text);

        if (dblNum2 == 0)
            Response.Write("<script>alert('You cannot divide by zero')</script>");
        else
        txtResult.Text = Convert.ToString(dblNum1 / dblNum2);
    }

//Store Data
    protected void btnStoredata_Click(object sender, EventArgs e)
    {
        Array.Resize(ref arrStore, arrStore.Length + 1);
        Int32 arrlength = arrStore.Length;
        arrStore.SetValue(txtResult.Text, arrlength - 1);
        Label1.Text = arrStore[0];
        if (arrStore[0] != null)
        Label2.Text = "Null";
        else
            Label2.Text = arrStore[1];
        Session["Store"] = arrStore;   
    }

//Print Data
    protected void btnPrintdata_Click(object sender, EventArgs e)
    {
        lstPrintdata.Items.Clear();
        lstPrintdata.Visible = true;
        for (int i = 0; i < arrStore.Length; ++i)
        {
            lstPrintdata.Items.Add(arrStore[i]);
        }
    }
}




Is This A Good Question/Topic? 0
  • +

Replies To: Save textbox to array in session using C# code behind

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 5059
  • View blog
  • Posts: 13,654
  • Joined: 18-April 07

Re: Save textbox to array in session using C# code behind

Posted 27 December 2015 - 10:43 PM

Look at line 26. This line is setting a "local" variable named arrStore, not the one you have defined back on line 14. I think you are wanting to pull the values out of Session["Store"] and store it in the variable on line 14 to then work with. Line 26 is just setting a local variable and then throwing it away when the Page_load ends. So start your journey there in finding the answer. :)
Was This Post Helpful? 1
  • +
  • -

#3 lboyce72  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 18-August 15

Re: Save textbox to array in session using C# code behind

Posted 10 January 2016 - 07:56 PM

Thank you, I was able to get it working, now to focus on my VB version.

Larry
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1