So I'm trying to make a simple banking website using C# and ASP.net and I'm just learning about Session Variables for the first time. I have a starting account balance of 1000 dollars and I want to transfer that to another page and have that balance updated via withdrawals or deposits and then update the balance. Heres my code:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace Project4.Forms { public partial class Services : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void btnSubmit_Click1(object sender, EventArgs e) { double userInput = double.Parse(txtAmount.Text); Session["userInput"] = userInput; Session["balance"] = 1000.00; double balance = Convert.ToDouble(Session["balance"]); Session["userAction"] = ddlSelectService.SelectedItem.Text; if (ddlSelectService.SelectedItem.Text == "Withdrawl") { balance -= userInput; } else if (ddlSelectService.SelectedItem.Text == "Deposit") { balance += userInput; } else { } Response.Redirect("Status.aspx"); } } } I understand the root of my problem is probably the original Session["balance"] = 1000.00; but I'm not sure how to declare a starting amount and then it not affect the balance when I return to this page from the results page. Any help is appreciated, also as I said in the beginning this is a barebones simple atm website please don't suggest anything too crazy because I am a beginner.
if (Session["balance"] == null) Session["balance"] = 1000;. Also, seems like this should be in thePage_Loadevent, not thebtnSubmit_Clickevent, since it only has to happen once.