将记录添加到数据库时出现空错误

时间:2013-10-29 19:37:17

标签: c# visual-studio-2010 ado.net

我有以下应用程序根据搜索加载客户记录。然后可以使用显示的字段添加新发票。

但是当我去添加新的发票记录时,我在invoiceNumber列上得到了“noNullAllowedException”。我不知道为什么我会在添加新发票时收到此错误,例如我将默认值-1更改为“11”并更改其他默认发票选项。

我检查了列属性以检查它是否来自冲突的设置,但似乎很好。有人知道我在哪里验证验证,它是否给出了这个结果?

Default form settings:

Invoice user input:

Resulting error:

Dataset schema

这是发票添加方法背后的代码:

private void addInvoice_Click(object sender, EventArgs e)
{
   // Check that the line item data is valid
   if (IsValidLineItem())
   {
      // Add a new row to the InvoiceLineItems table
      lineItemsBindingSource.AddNew();

      // Set the values of the row in the data grid
      int rowIndex = lineItemsDataGridView.Rows.Count - 1;
      DataGridViewRow row = lineItemsDataGridView.Rows[rowIndex];

      DataGridViewCell cell = row.Cells[0];
      cell.Value = 1;
      cell = row.Cells[1];
      cell.Value = productIDComboBox.SelectedValue; // not Index
      cell = row.Cells[2];
      cell.Value = unitPriceTextBox.Text;
      cell = row.Cells[3];
      cell.Value = quantityTextBox.Text;
      cell = row.Cells[4];
      cell.Value = Convert.ToDecimal(unitPriceTextBox.Text) * Convert.ToDecimal(quantityTextBox.Text);    

      // Save the line item to the table
      lineItemsBindingSource.EndEdit();

      // Calculate the invoice total
      this.GetInvoiceTotal();

      // Prepare for the next entry
      quantityTextBox.Text = "";
      quantityTextBox.Focus();
   }
}

1 个答案:

答案 0 :(得分:0)

显然,其中一项任务是将单元格值设置为 null

cell.Value = productIDComboBox.SelectedValue; // not Index
...
cell.Value = unitPriceTextBox.Text;
...
cell.Value = quantityTextBox.Text;

如果你知道哪一列是“InvoiceNumber”,那么你将有罪魁祸首。

相关问题