是否可以在列数据类型为double的列中插入空值?
本文关键字:插入 空值 double 数据类型 是否 | 更新日期: 2023-09-27 18:17:00
`enter code here`
private void btnSave_Click(object sender, RoutedEventArgs e)
{
GRepository<tblDEIPO_Primary> GR = new GRepository<tblDEIPO_Primary>();
tblDEIPO_Primary objDIE = new tblDEIPO_Primary();
if (txtid.Text == "")
{
objDIE.DIEPONo = txtDIEPO.Text;
objDIE.PoDate = dtpDeiPoDate.Text;
objDIE.Vendor = cmbVendor.Text;
objDIE.Remark = txtRemark.Text;
objDIE.TermsAndConditions = txtTermsAndCondition.Text;
objDIE.TaxType = cmbTaxtype.Text;
objDIE.Tax = Convert.ToDouble(txtTax.Text);
objDIE.TaxAmount = Convert.ToDouble(txtTaxAmount.Text);
objDIE.BonusPenalty = s;
if (chkBonus.IsChecked == true)
{
objDIE.BonusPenaltyAmount = Convert.ToDouble(txtBonus.Text);
}
else if (chkPenalty.IsChecked == true)
{
objDIE.BonusPenaltyAmount = Convert.ToDouble(txtPenalty.Text);
}
objDIE.DeiType = cmbDieType.Text;
objDIE.TypeofInjections = cmbTypeOfInjection.Text;
objDIE.Total =Convert.ToDouble( txtTotal.Text);
objDIE.TotalAmount =Convert.ToDouble(txtAmount.Text);
GR.Insert(objDIE);
GR.Save();}
当我点击btnSave_Click按钮时,时间错误产生"输入字符串格式不正确。"在objDIE。Tax = Convert.ToDouble(txtax . text);因为我正在这个文本框中插入空白值,所以请提供解决方案。
> Blockquote
所以使用double.TryParse
double taxAmount= 0;
objDIE.TaxAmount = double.TryParse(txtTaxAmount.Text, out taxAmount) ? taxAmount: DbNull.Value;
您可以使用double.TryParse
来测试您的输入值是否为double。
可以替换为:
objDIE.TaxAmount = Convert.ToDouble(txtTaxAmount.Text);
double num;
bool res = double.TryParse(txtTax.Text, out num);
if (res)
{
// String is a double.
objDIE.TaxAmount = num;
}