-3

The code below keeps giving me a stackoverflowexception.

I've a form_Load() function which clears all values on load. then I add code to fill in the values from the database and get the error.

I'm trying the get product from the database and fill in the textbox in winforms application.

The code looks like this:

private void txtNetAmount_TextChanged(object sender, EventArgs e)
    {
        try
        {
            decimal dcNetAmount = 0;
            decimal dcRate = 0;
            decimal dcWeight = 0;
            decimal dcGrossAmount;
            decimal dcMaking = 0;
            decimal dcDicount = 0;
            //TODO: calculate stone price
            //TaxAmountCalculation();

            if (EditingMakingAmount == false && EditingDiscountAmount == false)
            {
                if (txtNetAmount.Text.Trim() != string.Empty)
                {
                    dcNetAmount = Convert.ToDecimal(txtNetAmount.Text.Trim());
                }
                if (txtRate.Text.Trim() != string.Empty)
                {
                    dcRate = Convert.ToDecimal(txtRate.Text.Trim());
                }
                if (txtWeight.Text.Trim() != string.Empty)
                {
                    dcWeight = Convert.ToDecimal(txtWeight.Text.Trim());
                }
                dcGrossAmount = dcRate * dcWeight;
                if (dcNetAmount > dcGrossAmount)
                {
                    dcMaking = dcNetAmount - dcGrossAmount;
                }
                else if (dcNetAmount < dcGrossAmount)
                {
                    dcDicount = dcGrossAmount - dcNetAmount;
                }
                txtMaking.Text = Math.Round(dcMaking, PublicVariables._inNoOfDecimalPlaces).ToString();
                txtDiscountAmount.Text = Math.Round(dcDicount, PublicVariables._inNoOfDecimalPlaces).ToString();
                txtAmount.Text = Math.Round(dcNetAmount, PublicVariables._inNoOfDecimalPlaces).ToString();
            }
        }
        catch (Exception ex)
        {
            formMDI.infoError.ErrorString = "POS88:" + ex.Message;
        }
    }

In the above code the exception is at the following line:

txtMaking.Text = Math.Round(dcMaking, PublicVariables._inNoOfDecimalPlaces).ToString();

C# StackOverFlowException keeps hitting me with this code

I can't see the problem. PS the application is being developed in winforms.

Dale K
  • 25,246
  • 15
  • 42
  • 71
Dev5G
  • 82
  • 7
  • 4
    If this code *just* contains the line assigning `txtMaking.Text`, does it still exhibit the problem? If so, does `txtMaking` *also* have a `TextChanged` event handler? The obvious thing to look for is if you've got a set of text boxes that all have `TextChanged` event handlers that are mutually cross-setting each other. – Damien_The_Unbeliever Mar 18 '19 at 10:32
  • 2
    Do you have a cyclic dependency between `txtMaking` and `txtNetAmount`? May be a event handler for `txtMaking` which in turn changes value for `txtNetAmount`. Or if in any way both the values are dependent of each other? – praty Mar 18 '19 at 10:32
  • 1
    It's bad coding to ever write `catch (Exception ex)`. You should only ever catch **specific** exceptions that you can **meaningfully** handle. – Enigmativity Mar 18 '19 at 10:33
  • Yes txtMaking also has a TextChanged event handler – Dev5G Mar 18 '19 at 10:34
  • i'll try to avoid that but for now my focus is this exception @Enigmativity – Dev5G Mar 18 '19 at 10:35
  • 3
    @ShahidHussain so that's the problem you have a `cyclic dependency` between txtMaking and txtNetAmount – styx Mar 18 '19 at 10:37
  • So set breakpoints at the top of this handler and the `txtMaking` handler and debug - watch the callstack window and how it grows each time you hit these breakpoints. That's your stack overflow happening. We'd need to see different code (strip out irrelevant, add other relevant parts of the other handlers) and have a clearer understanding of *what you're trying to do* to suggest *fixes*. – Damien_The_Unbeliever Mar 18 '19 at 10:43
  • @ShahidHussain Are you setting the `Text` property of the `txtMaking` control inside the `txtMaking_TextChanged` event? Can you see why that might be a problem? – DavidG Mar 18 '19 at 10:56
  • Thank you @styx and @ Damien_The_Unbeliever i just got it working again... – Dev5G Mar 18 '19 at 10:56
  • @ShahidHussain, so whats the problem? – er-sho Mar 18 '19 at 10:57
  • @DavidG Yes that was the problem you described and i fixed it now.. – Dev5G Mar 18 '19 at 10:58
  • 1
    @er-sho well, the problem was actually the two mutually connected text boxes which were changing values before even initializing the variables... – Dev5G Mar 18 '19 at 11:01
  • 1
    @er-sho i added a Boolean variable to check to see if the other TextChanged Propery was trigger.. and then calculated the values – Dev5G Mar 18 '19 at 11:02
  • @ShahidHussain why are you using textboxes when working with numbers? Why not numericupdown? – Aleksa Ristic Mar 18 '19 at 11:05
  • @AleksaRistic To be honest i did not know about the numericUpDown control when i start developing the application but i've started the implementation and also wanted to know the reason of the error.. which i found out.. Thanks to the help of people who commented above.. :-) – Dev5G Mar 18 '19 at 11:12
  • @Damien_The_Unbeliever Can you tell me why this question is off-topic .? – Dev5G Mar 18 '19 at 11:13
  • Don't just read the first few words of the on-hold box. Read the whole thing. It explains what we would want in a good question here. – Damien_The_Unbeliever Mar 18 '19 at 12:54
  • @Damien_The_Unbeliever Uhh.. Thank you bro.. i'll edit it... and tell you what the problem was and how it can be reproduced... :-) – Dev5G Mar 18 '19 at 13:18
  • Look at [this](https://stackoverflow.com/questions/326223/overriding-fields-or-properties-in-subclasses) it may be usefull to you. Events `TextChanged` and other's are fired through properties. It may be useful to you to know that you can override some property so maybe do not fire event at all. – Aleksa Ristic Mar 18 '19 at 17:33
  • @AleksaRistic thank you .. I am learning as i face different problems – Dev5G Mar 20 '19 at 10:57

1 Answers1

1

The Problem in the code was simple yet difficult to see ...

txtNetWeight_TextChanged

The above code changes the value of txtMaking

txtMaking_TextChanged 

while this handler changes the txtNetWeight value ,, and that began a recursive loop..

So I added a variable (Boolean) to check if the other property was changed or not. Then allowed only one the TextChanged properties to set values for-each textbox.

Dale K
  • 25,246
  • 15
  • 42
  • 71
Dev5G
  • 82
  • 7