Error with Adding multiple labels to get desired output in another label control

Donald Symmons 2,881 Reputation points
2021-07-17T16:42:20.017+00:00

Here is the full description of what I am trying to achieve.

The way I designed it is that I have 3 Checkboxes, 3 DropDowns controls and 7 Label controls. The DropDowns are for Quantity, Labels are for Unit Price, SubTotal and GrandTotal. 3 Labels for Unit Price, 3 Labels for SubTotal and one Label for GrandTotal. On page load, DropDown will be disabled, Unit Price Labels will have values as "100", "200" and "100" respectively and the 3 SubTotal Labels will have initial values as "0", and GrandTotal Label will be "0". When a user clicks on any of the Checkboxes, the DropDown attached to that clicked Checkbox will be enabled and when user selects quantity from DropDown, the value of Unit Price Label will be multiplied by the value in the quantity DropDown and output the result in SubTotal Labels. Finally, all the values of SubTotal Labels will be added and the result will be displayed in the GrandTotal Label.

This is how the view will look like

Product Unit Price Quantity SubTotal GrandTotal

Product1 100 3 300

Product2 200 1 200

Product3 100 2 200 700

I tried to calculate the total by adding the subtotals but it's not working.
Please I need help in solving this. Thank you

Here is my C# code

protected void Page_Load(object sender, EventArgs e)
{
    unitprice1.Text = ("100").ToString();
    unitprice2.Text = ("200").ToString();
    unitprice3.Text = ("100").ToString();

   LabelTOTAL.Text = (int.Parse(Subtotal1.Text) + int.Parse(Subtotal2.Text) + int.Parse(Subtotal3.Text)).ToString();

}

}

protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
    Product1DropDown.Enabled = CheckBox1.Checked;
    Subtotal1.Text = ("0").ToString();
}

protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
{
    Product2DropDown.Enabled = CheckBox2.Checked;
    Subtotal2.Text = ("0").ToString();
}

protected void CheckBox3_CheckedChanged(object sender, EventArgs e)
{
    Product3DropDown.Enabled = CheckBox3.Checked;
    Subtotal3.Text = ("0").ToString();
}

protected void Product1DropDown_SelectedIndexChanged(object sender, EventArgs e)
{
    if (Product1DropDown.SelectedIndex > 0)
    {
        Qty1.Text = Product1DropDown.SelectedItem.Text;
        Subtotal1.Text = Convert.ToString(Convert.ToInt32(Qty1.Text) * Convert.ToInt32(unitprice1.Text)).ToString();
    }
}

protected void Product2DropDown_SelectedIndexChanged(object sender, EventArgs e)
{
    if (Product2DropDown.SelectedIndex > 0)
    {
        Qty2.Text = Product2DropDown.SelectedItem.Text;
        Subtotal2.Text = Convert.ToString(Convert.ToInt32(Qty2.Text) * Convert.ToInt32(unitprice2.Text)).ToString();
    }
}

protected void Product3DropDown_SelectedIndexChanged(object sender, EventArgs e)
{
    if (Product3DropDown.SelectedIndex > 0)
    {
        Qty3.Text = Product3DropDown.SelectedItem.Text;
        Subtotal3.Text = Convert.ToString(Convert.ToInt32(Qty3.Text) * Convert.ToInt32(unitprice3.Text)).ToString();
    }
}
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,927 questions
Small BASIC
Small BASIC
A programming language created by Microsoft that serves a stepping stone for beginners from block-based coding languages to more complex text-based languages.
280 questions
{count} votes

Accepted answer
  1. Donald Symmons 2,881 Reputation points
    2021-07-19T11:45:27.733+00:00

    I tried this and it's working for me.

    protected void Page_Load(object sender, EventArgs e)
            {
                //set values
    
                lblProduct1UnitPrice.Text = "100";
                lblProduct2UnitPrice.Text = "200";
                lblProduct3UnitPrice.Text = "100";
    
                //populate DropDownList
                for(int i = 0; i < 100; i++)
                {
                    if (i > 0)
                    {
                        //add
    
                        product1DropDown.Items.Add(i.ToString());
                        product2DropDown.Items.Add(i.ToString());
                        product3DropDown.Items.Add(i.ToString());
                    }
                    else
                    {
                        //add - for 0, show empty (blank)
                        product1DropDown.Items.Add("");
                        product2DropDown.Items.Add("");
                        product3DropDown.Items.Add("");
                    }
                }
            }
    
            private void CalculateGrandTotal()
            {
                decimal subtotal1 = 0m; //use 'm' for decimal
                decimal subtotal2 = 0m; //use 'm' for decimal
                decimal subtotal3 = 0m; //use 'm' for decimal
    
                try
                {
                    //calculate subtotal1 - only calculate if CheckBox is enabled
                    if (checkBox1.Enabled && !String.IsNullOrEmpty(product1DropDown.Text))
                    {
                        //calculate subtotal
                        subtotal1 = CalculateSubtotal(product1DropDown.Text, lblProduct1UnitPrice.Text);
                    }
    
                    //set value
                    lblProduct1Subtotal.Text = subtotal1.ToString("$###,##0.00");
                }
                catch(Exception ex)
                {
                    //ToDo: log msg
    
                    //set value
                    lblMsg.Text = "Error (CalculateGrandTotal 1) - " + ex.Message;
    
                    //uncommenting the following line may be useful for debugging
                    //throw ex;
                }
    
                try
                {
                    //calculate subtotal 2 - only calculate if CheckBox is enabled
                    if (checkBox2.Enabled && !String.IsNullOrEmpty(product2DropDown.Text))
                    {
                        //calculate subtotal
                        subtotal2 = CalculateSubtotal(product2DropDown.Text, lblProduct2UnitPrice.Text);
                    }
    
                    //set value
                    lblProduct2Subtotal.Text = subtotal2.ToString("$###,##0.00");
                }
                catch(Exception ex)
                {
                    //ToDo: log msg
    
                    //set value
                    lblMsg.Text = "Error (CalculateGrandTotal 2) - " + ex.Message;
    
                    //uncommenting the following line may be useful for debugging
                    //throw ex;
                }
    
                try
                {
                    //calculate subtotal3 - only calculate if CheckBox is enabled
                    if (checkBox3.Enabled && !String.IsNullOrEmpty(product3DropDown.Text))
                    {
                        //calculate subtotal
                        subtotal3 = CalculateSubtotal(product3DropDown.Text, lblProduct3UnitPrice.Text);
                    }
    
                    //set value
                    lblProduct3Subtotal.Text = subtotal3.ToString("$###,##0.00");
                }
                catch(Exception ex)
                {
                    //ToDo: log msg
    
                    //set value
                    lblMsg.Text = "Error (product3DropDown_SelectedIndexChanged) - " + ex.Message;
    
                    //uncommenting the following line may be useful for debugging
                    //throw ex;
                }
    
                //calculate
                decimal grandTotal = subtotal1 + subtotal2 + subtotal3;
    
                //set value
                lblGrandTotal.Text = grandTotal.ToString("$###,##0.00");
            }
    
            private decimal CalculateSubtotal(string strQty, string strUnitPrice)
            {
                int qty = 0;
                decimal unitPrice = 0m;
                decimal subtotal = 0m;
    
                //clear msg
                lblMsg.Text = string.Empty;
    
                if (!String.IsNullOrEmpty(strQty))
                {
                    //try to convert
                    Int32.TryParse(strQty, out qty);
                }
                else
                {
                    throw new Exception("Error: Qty is null or empty");
                }
    
                if (!String.IsNullOrEmpty(strUnitPrice))
                {
                    //try to convert
                    Decimal.TryParse(strUnitPrice, out unitPrice);
    
                    //calculate
                    subtotal = qty * unitPrice;
                }
                else
                {
                    throw new Exception("Error: UnitPrice is null or empty");
                }
    
                return subtotal;
            }
    
            protected void checkBox1_CheckedChanged(object sender, EventArgs e)
            {
                ////uncomment for debugging
                //lblMsg.Text = "Info: checkBox1.Checked: " + checkBox1.Checked.ToString();
                //System.Diagnostics.Debug.WriteLine("Info: checkBox1.Checked: " + checkBox1.Checked.ToString());
    
                //set value
                product1DropDown.Enabled = checkBox1.Checked;
    
                if (!product1DropDown.Enabled)
                {
                    //not enabled - reset values
                    product1DropDown.Text = string.Empty;
                    lblProduct1Subtotal.Text = "$0.00";
                }
    
                //calculate
                CalculateGrandTotal();
            }
    
            protected void checkBox2_CheckedChanged(object sender, EventArgs e)
            {
                //uncomment for debugging
                //lblMsg.Text = "Info: checkBox2.Checked: " + checkBox2.Checked.ToString();
    
                //set value
                product2DropDown.Enabled = checkBox2.Checked;
    
                if (!product2DropDown.Enabled)
                {
                    //not enabled - reset values
                    product2DropDown.Text = string.Empty;
                    lblProduct2Subtotal.Text = "$0.00";
                }
    
                //calculate
                CalculateGrandTotal();
            }
    
            protected void checkBox3_CheckedChanged(object sender, EventArgs e)
            {
                //uncomment for debugging
                //lblMsg.Text = "Info: checkBox3.Checked: " + checkBox3.Checked.ToString();
    
                //set value
                product3DropDown.Enabled = checkBox3.Checked;
    
                if (!product3DropDown.Enabled)
                {
                    //not enabled - reset values
                    product3DropDown.Text = string.Empty;
                    lblProduct3Subtotal.Text = "$0.00";
                }
    
                //calculate
                CalculateGrandTotal();
            }
    
            protected void product1DropDown_SelectedIndexChanged(object sender, EventArgs e)
            {
                CalculateGrandTotal();
            }
    
            protected void product2DropDown_SelectedIndexChanged(object sender, EventArgs e)
            {
                CalculateGrandTotal();
            }
    
            protected void product3DropDown_SelectedIndexChanged(object sender, EventArgs e)
            {
                CalculateGrandTotal();
            }
        }
    }
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Viorel 117.4K Reputation points
    2021-07-17T17:35:53.843+00:00

    Try adding these lines to Page_Load:

    EventHandler eh = ( s, a ) =>
    {
       LabelTOTAL.Text = ( int.Parse( Subtotal1.Text ) + int.Parse( Subtotal2.Text ) + int.Parse( Subtotal3.Text ) ).ToString( );
    };
    
    Subtotal1.TextChanged += eh;
    Subtotal2.TextChanged += eh;
    Subtotal3.TextChanged += eh;
    

    or these:

    new[] { Subtotal1, Subtotal2, Subtotal3 }
        .All( c =>
        {
           c.TextChanged += ( s, a ) =>
           {
              LabelTOTAL.Text = ( int.Parse( Subtotal1.Text ) + int.Parse( Subtotal2.Text ) + int.Parse( Subtotal3.Text ) ).ToString( );
           };
           return true;
        } );
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.