0

以下代码在看似随机的时间提示 tryparse 的消息,然后使用空值或 null 值更新我的数据。这可能在数据输入到 totaltaxtextbox 和 totalwatertextbox 时发生。

    private void button2_Click(object sender, EventArgs e)
    {

            string parcel = parcelNumberTextBox.Text;
            string premises = premisesTextBox.Text;
            string beg = assessedBegTextBox.Text;
            string dim = assessedDimTextBox.Text;
            string legal = legalTextBox.Text;
            string notes = sumNotesTextBox.Text;
            string taxf = taxFromTextBox.Text;
            string taxt = taxToTextBox.Text;
            string waterf = waterFromTextBox.Text;
            string watert = waterToTextBox.Text;
            string recital = recitalTextBox.Text;
            string taxLien = totalTaxTextBox.Text;
            string waterLien = totalWaterTextBox.Text;
            string taxName = taxAcctNameTextBox.Text;
            string registry = registryTextBox.Text;

            decimal taxOpen;
            if (!decimal.TryParse(taxOpenTextBox.Text, NumberStyles.Number | NumberStyles.AllowCurrencySymbol,
                     NumberFormatInfo.CurrentInfo, out taxOpen))
                MessageBox.Show("Please enter a valid number in the Tax Open Text Box");

            decimal waterOpen;

            if (!decimal.TryParse(waterOpenTextBox.Text, NumberStyles.Number | NumberStyles.AllowCurrencySymbol,
                     NumberFormatInfo.CurrentInfo, out waterOpen))
                MessageBox.Show("Please enter a valid number in the Water Open Text Box");


            DataClasses1DataContext db = new DataClasses1DataContext();

            var matchedCaseNumber = (from c in db.GetTable<PropertyInformation>()
                                     where c.CaseNumberKey == caseNumberKeyTextBox.Text
                                     select c).SingleOrDefault();


            matchedCaseNumber.AssessedBeg = beg;
            matchedCaseNumber.AssessedDim = dim;
            matchedCaseNumber.Premises = premises;
            matchedCaseNumber.Legal = legal;
            matchedCaseNumber.SumNotes = notes;
            matchedCaseNumber.ParcelNumber = parcel;
            matchedCaseNumber.TaxFrom = taxf;
            matchedCaseNumber.TaxTo = taxt;
            matchedCaseNumber.WaterFrom = waterf;
            matchedCaseNumber.WaterTo = watert;
            matchedCaseNumber.TaxOpen = taxOpen;
            matchedCaseNumber.WaterOpen = waterOpen;
            matchedCaseNumber.Recital = recital;
            matchedCaseNumber.TotalTax = taxLien;
            matchedCaseNumber.TotalWater = waterLien;
            matchedCaseNumber.Registry = registry;
            matchedCaseNumber.TaxAcctName = taxName;
            matchedCaseNumber.TaxOpen = taxOpen;
            matchedCaseNumber.WaterOpen = waterOpen;

            db.SubmitChanges();
            Dirty = false;

    }

有关如何解决此问题的任何帮助都会很棒。

谢谢!

4

1 回答 1

4

那是因为您的代码只是在显示 MessageBox 后继续执行

if (!decimal.TryParse(taxOpenTextBox.Text, NumberStyles.Number | NumberStyles.AllowCurrencySymbol,
                 NumberFormatInfo.CurrentInfo, out taxOpen))
{
     MessageBox.Show("Please enter a valid number in the Tax Open Text Box");
     return;   // <--------------
}

顺便说一句,处理此问题的更好方法是使用errorProvider,INotifyPropertyChangedvalidation.

于 2011-05-16T00:37:33.097 回答