My assignment is to create an application that allows the user to type an item's price into a single textbox, add the price to the previously entered prices, and display the total charge which includes a $15 shipping charge and a 3% sales tax.

Example: I type 10 in the textbox and click calculate. the subtotal (before tax and shipping) reads $10. Next, I type 20 in the textbox and click calculate. The subtotal should now read $30.

I have everything working fine except adding the different prices. I cannot figure out how to retain the value of the subtotal and add it to the next value entered.

Dani AI

Generated

A simple, robust approach for an ASP.NET Web Forms page is to keep a running subtotal on the server between postbacks and add each new entry to it. is right that the total must persist between clicks, and is right that the operation itself is just an add — the missing piece is where you keep the running subtotal. The snippet below uses ViewState so the subtotal survives postbacks without using Session or a hidden HTML field.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not IsPostBack Then
        ViewState("Subtotal") = 0D
    End If
End Sub

Protected Sub btnCalculate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnCalculate.Click
    Dim price As Decimal
    If Decimal.TryParse(txtPrice.Text.Trim(), price) Then
        Dim subtotal As Decimal = If(ViewState("Subtotal") IsNot Nothing, CType(ViewState("Subtotal"), Decimal), 0D)
        subtotal += price
        ViewState("Subtotal") = subtotal

        Dim tax As Decimal = Math.Round(subtotal * 0.03D, 2)
        Dim shipping As Decimal = 15D
        Dim total As Decimal = subtotal + tax + shipping

        lblSubtotal.Text = subtotal.ToString("C2")
        lblTax.Text = tax.ToString("C2")
        lblShipping.Text = shipping.ToString("C2")
        lblTotal.Text = total.ToString("C2")
    Else
        lblError.Text = "Enter a valid price."
    End If
End Sub

Notes and troubleshooting: use Decimal for currency to avoid floating-point rounding errors and use Decimal.TryParse to validate input. If the subtotal keeps resetting, verify If Not IsPostBack logic, ensure ViewState is enabled for the page and controls, and confirm event handlers are wired (control IDs match). For multi-page persistence or large data sets prefer Session or a database; ViewState adds page payload and should not hold sensitive info. See the ASP.NET page lifecycle and ViewState concepts for more context: and the .NET Decimal docs for currency tips: System.Decimal.

Recommended Answers

All 2 Replies

Really? Store the current total as a variable. Add the value of the textbox to it and then output the new total to the textbox. If you're having trouble keeping the current total persisting (you don't mention if this is a webpage or not) use session state or a hidden variable as part of the page.

You shouldn't even need a variable.
Add textbox value to subtotal value and you're done.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.