0

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.

4
  • 2
    Off topic but may I ask why you are learning a framework currently considered by many as LTS? Is this required for school? If you have no good reason to start here I would scrap this and learn something newer that will be of more use to you. If you want server side code try the latest .net framework with MVC. If you want client side code learn pure HTML and javascript. If you want a client side framework learn angular or react. Commented Jul 29, 2021 at 19:23
  • Yes, it is for school. Commented Jul 29, 2021 at 19:25
  • 1
    I don't do asp.net, so this is just a guess, but maybe try only setting the default value if it's null, something like: if (Session["balance"] == null) Session["balance"] = 1000;. Also, seems like this should be in the Page_Load event, not the btnSubmit_Click event, since it only has to happen once. Commented Jul 29, 2021 at 19:29
  • Thanks!, that + setting the session variable = balance at the end seemed to work. Commented Jul 29, 2021 at 19:41

1 Answer 1

1

I don't do asp.net, so this is just a guess, but maybe try only setting the default value if it's null, something like: if (Session["balance"] == null) Session["balance"] = 1000;. It also seems like this should be in the Page_Load event, not the btnSubmit_Click event, since it only has to happen once.

Finally, don't forget to update the session variable after setting the new balance:

protected void Page_Load(object sender, EventArgs e) { if (Session["balance"] == null) Session["balance"] = 1000; } protected void btnSubmit_Click1(object sender, EventArgs e) { double userInput; if (!double.TryParse(txtAmount.Text, out userInput)) { ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('Input must be a valid double');", true); return; } Session["userInput"] = userInput; double balance = Convert.ToDouble(Session["balance"]); Session["userAction"] = ddlSelectService.SelectedItem.Text; if (Session["userAction"] == "Withdrawl") { balance -= userInput; } else if (Session["userAction"] == "Deposit") { balance += userInput; } Session["balance"] = balance; Response.Redirect("Status.aspx"); } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.