我收到错误“索引或主键不能包含空值”从下面的代码 [英] I get an error "index or primary key cannot contain a null value" from code below
本文介绍了我收到错误“索引或主键不能包含空值”从下面的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
If inc <> -1 And txtID.Text <> Then
Dim cb As New OleDb.OleDbCommandBuilder(da)
Dim dsNewRow As DataRow
dsNewRow = ds.Tables("IBCARIP").NewRow()
ds.Tables("IBCARIP").Rows(inc).Item("EmpCode") = txtID.Text
ds.Tables("IBCARIP").Rows(inc).Item("IDNumber") = txtIDN.Text
ds.Tables("IBCARIP").Rows(inc).Item("FName") = txtName.Text
ds.Tables("IBCARIP").Rows(inc).Item("SName") = txtSName.Text
ds.Tables("IBCARIP").Rows(inc).Item("Jtitle") = txtJD.Text
ds.Tables("IBCARIP").Rows(inc).Item("Initials") = txtIn.Text
ds.Tables("IBCARIP").Rows(inc).Item("Gender") = cmbSex.Text
ds.Tables("IBCARIP").Rows(inc).Item("Title") = cmbTt.Text
ds.Tables("IBCARIP").Rows(inc).Item("Age") = txtAge.Text
ds.Tables("IBCARIP").Rows(inc).Item("PAddress") = txtPAd.Text
ds.Tables("IBCARIP").Rows(inc).Item("POAddress") = txtPOAd.Text
ds.Tables("IBCARIP").Rows(inc).Item("CNumber") = txtCNos.Text
ds.Tables("IBCARIP").Rows(inc).Item("Ailments") = txtAil.Text
ds.Tables("IBCARIP").Rows(inc).Item("NKName") = txtNK.Text
ds.Tables("IBCARIP").Rows(inc).Item("NKAddress") = txtNKAddress.Text
ds.Tables("IBCARIP").Rows(inc).Item("NKNumbers") = txtNKNos.Text
ds.Tables("IBCARIP").Rows(inc).Item("NRate") = txtNR.Text
ds.Tables("IBCARIP").Rows(inc).Item("OTRate") = txtOTR.Text
ds.Tables("IBCARIP").Rows(inc).Item("BName") = cmbBnk.Text
ds.Tables("IBCARIP").Rows(inc).Item("ANumber") = txtAN.Text
ds.Tables("IBCARIP").Rows(inc).Item("AType") = cmbAT.Text
ds.Tables("IBCARIP").Rows(inc).Item("BCode") = txtBC.Text
ds.Tables("IBCARIP").Rows(inc).Item("BBName") = txtBB.Text
ds.Tables("IBCARIP").Rows(inc).Item("Notes") = txtNotes.Text
ds.Tables("IBCARIP").Rows(inc).Item("MStatus") = cmbMS.Text
' da.Update(ds, "IBCARIP")
ds.Tables("IBCARIP").Rows.Add(dsNewRow)
da.Update(ds, "IBCARIP")<<<<<<<<ERROR index or primary key cannot contain a null value
MsgBox("New Record added to the Database")
Else
MsgBox("Please ensure all important fields are entered", MsgBoxStyle.OkOnly)
End If
End Sub
推荐答案
i was assigning values from textboxes to ds.Tables("IBCARIP").Rows(inc)instead of dsNewRow row: The correct solution
dsNewRow.Item("EmpCode") = txtID.Text
dsNewRow.Item("IDNumber") = txtIDN.Text
dsNewRow.Item("FName") = txtName.Text
dsNewRow.Item("SName") = txtSName.Text
dsNewRow.Item("Jtitle") = txtJD.Text
dsNewRow.Item("Initials") = txtIn.Text
dsNewRow.Item("Gender") = cmbSex.Text
dsNewRow.Item("Title") = cmbTt.Text
dsNewRow.Item("Age") = txtAge.Text
dsNewRow.Item("PAddress") = txtPAd.Text
dsNewRow.Item("POAddress") = txtPOAd.Text
dsNewRow.Item("CNumber") = txtCNos.Text
dsNewRow.Item("Ailments") = txtAil.Text
dsNewRow.Item("NKName") = txtNK.Text
dsNewRow.Item("NKAddress") = txtNKAddress.Text
dsNewRow.Item("NKNumbers") = txtNKNos.Text
dsNewRow.Item("NRate") = txtNR.Text
dsNewRow.Item("OTRate") = txtOTR.Text
dsNewRow.Item("BName") = cmbBnk.Text
dsNewRow.Item("ANumber") = txtAN.Text
dsNewRow.Item("AType") = cmbAT.Text
dsNewRow.Item("BCode") = txtBC.Text
dsNewRow.Item("BBName") = txtBB.Text
dsNewRow.Item("Notes") = txtNotes.Text
dsNewRow.Item("MStatus") = cmbMS.Text
你应该在数据库表primary中添加一个自动增量字段,如果是数字字段。
或者您应该将唯一值传递给主字段列。
you should put a auto increment in the database table primary field, if it is a number field.
Or you should pass a unique value to your primary field column.
这篇关于我收到错误“索引或主键不能包含空值”从下面的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文