是否可以在列数据类型为double的列中插入空值?

时间:2016-11-08 13:46:38

标签: c# sql wpf

     `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按钮时,错误ganerate&#34;输入字符串的格式不正确。&#34;在objDIE.Tax = Convert.ToDouble(txtTax.Text);                     因为我在这个文本框中插入空白值而不是该错误的任何解决方案Plz帮助。

                    > Blockquote

2 个答案:

答案 0 :(得分:2)

因此请使用UserForm1.show vbModeless

double.TryParse

答案 1 :(得分:0)

您可以使用double.TryParse来测试输入值是否为双倍。

您可以替换:

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