VB.Net-无法启用约束。一或多个行包含违反非空,唯一或外键约束的值

时间:2018-11-22 16:34:10

标签: vb.net dataset

我正在使用VB.Net创建图书馆自动化系统。在图书添加表单(UI)中,我想添加图书作者(tblBookDetails中的一个字段。但是添加图书作者是另一个UI。要在图书添加UI中添加图书作者,我有一个组合框来填充所有authorFirstName但值成员应为authorID。我想从BookAuthor表中填充图书作者的名字。运行时出现以下错误Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.

tblBookAuthor和tblBookDetails模式

tblBookAuthor(authorID, authorFirstName) // authorID - PK

tblBookDetails(accessionNo, authorId, title) // authorID - FK

这是我的代码

Private Sub frmAddBook_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        SqlDataAdapter1.Fill(DataSet51, "tblBookAuthor") // Highlighted as error
        dt = DataSet51.Tables("tblBookAuthor")

    End Sub

更新

作者姓名的getId的附加代码

Private Sub cboBookAuthor_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbAuthorId.SelectedIndexChanged
    Dim i As Integer

    For i = 0 To DataSet51.Tables("tblBookAuthor").Rows.Count - 1
        If cboBookAuthor.Text = DataSet51.Tables("tblBookAuthor").Rows(i).Item(1) Then
            txtBookAuthorID.Text = DataSet51.Tables("tblBookAuthor").Rows(i).Item(0)
            Exit For
        End If

    Next

End Sub

0 个答案:

没有答案