简体   繁体   中英

C# winform how textbox became null on button click second time?

In a form I have group Box which contains tab control with 4 tabs.

In the second tab I have some textboxes and before saving data I need to validate input entered in these textbxes. Please note my save button is in last tab.

The following test scenario works:

  • Invalid input in first textbox
  • Invalid input in second textbox
  • Click button

However, in the following test scenario an "Object refrence not set to an instance of a object" exception is thrown:

  • Invalid input in first textbox
  • Click button
  • Invalid input in second textbox
  • click button

I have placed break point and it shows that particular textbox is null. It happens with every textbox where I break order.

Please guide me where I am incorrect and how I can fix it.

Below is my code that I am running on button press.

 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);

        }





    }

Are you sure you mean to set the textbox itself to null not the .Text or other member?

txtUnitPrice = null;

Call it a hunch, but will these work better?

            txtUnitPrice.Text = null;
....
            txtProfitRatio.Text = null;

The problem occurs because you are setting your Textboxes to NULL-value.

else
{
    txtUnitPrice = null;
}

you should instead set the Text property to String.Empty like so:

else
{
    txtUnitPrice.Text = String.Empty;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM