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

眉间皱痕 提交于 2019-12-23 15:35:26

问题


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

        }





    }

回答1:


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



回答2:


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;


来源:https://stackoverflow.com/questions/4780819/c-sharp-winform-how-textbox-became-null-on-button-click-second-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!