在一个表单中,我有一个组 Box,其中包含带有 4 个选项卡的选项卡控件。
在第二个选项卡中,我有一些文本框,在保存数据之前,我需要验证在这些文本框中输入的输入。请注意我的保存按钮位于最后一个选项卡中。
以下测试场景有效:
- 第一个文本框中的输入无效
- 第二个文本框中的输入无效
- 点击按钮
但是,在以下测试场景中,会引发“对象引用未设置到对象实例”异常:
- 第一个文本框中的输入无效
- 点击按钮
- 第二个文本框中的输入无效
- 单击按钮
我已经放置了断点,它显示特定的文本框为空。我破坏顺序的每个文本框都会发生这种情况。
请指导我哪里不正确以及如何修复它。
下面是我在按下按钮时运行的代码。
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);
}
}
您确定要将文本框本身设置为 null 而不是.Text
或其他成员?
txtUnitPrice = null;
可以说这是一种预感,但这些效果会更好吗?
txtUnitPrice.Text = null;
....
txtProfitRatio.Text = null;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)