我陷入了奇怪的境地。我有一个包含 1 个 DateTimePicker 2 个文本框、1 个 Combobox 和一个 datagridview 的表单。我已经使用 VB.Net 设置了编码,它同时将来自文本框 DTPickers、Combobox 和所有 Datagridview 行的数据保存在 SQL 数据库的一行中。数据已成功保存但我收到此错误消息,尽管 AccountID 不为空。在建议 DBNull.Value 之前,我想在这里澄清一下,如果我使用此选项,它会在数据库中保存一个额外的行,从 DTpicker、文本框和组合框获取现有值。请帮我解决这个问题。
con.Open()
For Each row As DataGridViewRow In DataGridView1.Rows
cmd = New SqlCommand("Insert into Transs (Date,DocNo,CostCenter,TransactionID,AccountID,AccountName,AccountType,Debit,Credit,Text) values (@Date,@DocNo,@CostCenter,@TransactionID,@AccountID,@AccountName,@AccountType,@Debit,@Credit,@Text)", con)
cmd.Parameters.Add("@Date", SqlDbType.Date).Value = DateTimePicker1.Value
cmd.Parameters.AddWithValue("@DocNo", TextBox1.Text)
cmd.Parameters.AddWithValue("@CostCenter", ComboBox1.SelectedItem)
cmd.Parameters.AddWithValue("@TransactionID", TextBox2.Text)
cmd.Parameters.AddWithValue("@AccountID", row.Cells("Column1").Value)
cmd.Parameters.AddWithValue("@AccountName", row.Cells("Column2").Value)
cmd.Parameters.AddWithValue("@AccountType", row.Cells("Column3").Value)
cmd.Parameters.AddWithValue("@Debit", row.Cells("Column4").Value)
cmd.Parameters.AddWithValue("@Credit", row.Cells("Column5").Value)
cmd.Parameters.AddWithValue("@Text", row.Cells("Column6").Value)
Try
cmd.ExecuteNonQuery()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Next
con.Close()
MsgBox("Record saved successfully!")