C # winform, how does the text field become null when the button is pressed a second time?

In the form, I have a group box containing a tab control with 4 tabs.

In the second tab, I have several text fields, and before saving the data, I need to check the input entered into these text files. Please note that my save button is on the last tab.

The following test script works:

  • Invalid input in the first text box
  • Invalid input in second text box
  • Press button

However, in the following test scenario, an exception is thrown, "Object Exception Not Installed in the Object Instance":

  • Invalid input in the first text box
  • Press button
  • Invalid input in second text box
  • press the button

, , null. , .

, , , .

, .

 private void btnOrderSave_Click(object sender, EventArgs e)
    {
        SaveOrder();

    }

    private void SaveOrder()
    {
        try
        {
            decimal? _marketRate, _bankcharges, _portcharges, _handlingcharges, _othercharges, _taxratio, _profitratio;

            if (!String.IsNullOrEmpty(txtUnitPrice.Text))
            {

                if (valCtr.IsDecimal(txtUnitPrice.Text))
                {
                    _marketRate = Convert.ToDecimal(txtUnitPrice.Text);
                }
                else
                {
                    ErrorMessage("Rate is invalid");                        
                    return;
                }
            }
            else
            {
                txtUnitPrice = null;
            }
            if (!String.IsNullOrEmpty(txtProfitRatio.Text))
            {
                if (valCtr.IsDecimal(txtProfitRatio.Text))
                {
                    _marketRate = Convert.ToDecimal(txtProfitRatio.Text);
                }
                else
                {
                    ErrorMessage("Profit ratio is invalid");                        
                    return;
                }
            }
            else
            {
                txtProfitRatio = null;
            }



        }

        catch (Exception ex)
        {
            AlertMessage(ex.InnerException + " :" + ex.Message + " : " + ex.StackTrace + " : " + ex.Source);

        }





    }
+3
2

- , NULL.

else
{
    txtUnitPrice = null;
}

Text String.Empty :

else
{
    txtUnitPrice.Text = String.Empty;
}
+2

null, .Text ?

txtUnitPrice = null;

, ?

            txtUnitPrice.Text = null;
....
            txtProfitRatio.Text = null;
+3

Source: https://habr.com/ru/post/1787416/


All Articles