HELP! with an easy asp.net problem?

  • Thread starter Thread starter Vanilla Suede
  • Start date Start date
V

Vanilla Suede

Guest
I am trying to create a simple calculator application that makes use of session state, I can add the first two number but the answer always displays as zero, or i get an error
public partial class _Default : System.Web.UI.Page
{
int op1 = 0;
int op2 = 0;
int result = 0;
string op = "";

protected void Page_Load(object sender, EventArgs e)
{

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

TextBox1.Text = "0";
Session["op1"] = "";
Session["op2"] = "";
Session["op"] = "";
}
protected void btnTwo_Click(object sender, EventArgs e)
{
if (Session["op1"].ToString() == "")
{
Session["op1"] = "2";
}
else
Session["op2"] = "2";

TextBox1.Text = "2";
}
protected void btnThree_Click(object sender, EventArgs e)
{
if (Session["op1"].ToString() == "")
{
Session["op1"] = "3";
}
else
Session["op2"] = "3";

TextBox1.Text = "3";
}
protected void btnAdd_Click(object sender, EventArgs e)
{
Session["op"] = "+";
}


protected void Button1_Click(object sender, EventArgs e)
{
Session["op1"] = "";
Session["op2"] = "";
Session["op"] = "";
TextBox1.Text = "";
}
protected void btnSeven_Click(object sender, EventArgs e)
{
if (Session["op1"].ToString() == "")
{
Session["op1"] = "7";
}
else
Session["op2"] = "7";

TextBox1.Text = "7";
}
protected void btnEight_Click(object sender, EventArgs e)
{
if (Session["op1"].ToString() == "")
{
Session["op1"] = "8";
}
else
Session["op2"] = "8";

TextBox1.Text = "8";
}
protected void btnNine_Click(object sender, EventArgs e)
{
if (Session["op1"].ToString() == "")
{
Session["op1"] = "9";
}
else
Session["op2"] = "9";

TextBox1.Text = "9";

}
protected void btnDivide_Click(object sender, EventArgs e)
{
Session["op"] = "/";
}
protected void btnFour_Click(object sender, EventArgs e)
{
if (Session["op1"].ToString() == "")
{
Session["op1"] = "4";
}
else
Session["op2"] = "4";

TextBox1.Text = "4";

}
protected void btnFive_Click(object sender, EventArgs e)
{
if (Session["op1"].ToString() == "")
{
Session["op1"] = "5";
}
else
Session["op2"] = "5";

TextBox1.Text = "5";

}
protected void btnSix_Click(object sender, EventArgs e)
{
if (Session["op1"].ToString() == "")
{
Session["op1"] = "6";
}
else
Session["op2"] = "6";

TextBox1.Text = "6";

}
protected void btnMultiply_Click(object sender, EventArgs e)
{
Session["op"] = "*";
}
protected void btnOne_Click(object sender, EventArgs e)
{
if (Session["op1"].ToString() == "")
{
Session["op1"] = "1";
}
else
Session["op2"] = "1";

TextBox1.Text = "1";

}

protected void btnSubtract_Click(object sender, EventArgs e)
{
Session["op"] = "-";
}
protected void btnZero_Click(object sender, EventArgs e)
{
if (Session["op1"].ToString() == "")
{
Session["op1"] = "0";
}
else
Session["op2"] = "0";

TextBox1.Text = "0";

}
protected void btnEqual_Click(object sender, EventArgs e)
{
// retrive op1 and op2 and operator, do calculation
//op1 = Convert.ToInt32(Session["op1"].ToString());
//op1 = Session["op1"].ToString();

//op2 = Convert.ToInt32(Session["op2"].ToString());
//op2 = Session["op2"].ToString();


if (Session["op"].ToString() == "+")
{
result = op1 + op2;
TextBox1.Text = result.ToString();
}
else if (Session["op"].ToString() == "-")
{
result = op1 - op2;
TextBox1.Text = result.ToString();
}
else if (Session["op"].ToString() == "*")
{
result = op1 * op2;
TextBox1.Text = result.ToString();
}
else if (Session["op"].ToString() == "/")
{
result = op1 / op2;
TextBox1.Text = result.ToString();
}



}
 
Back
Top