无法删除或更改记录,因为表'模块'包含相关记录

时间:2017-04-03 17:48:15

标签: vb.net ms-access exception datagridview

删除记录时遇到问题,此异常不断发生。

  

System.Data.dll中出现未处理的“System.Data.OleDb.OleDbException”类型异常

     

附加信息:由于“模块”表包含相关记录,因此无法删除或更改记录。

Public Class frmStudentDatabase

    Dim objConnection As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;" & "Data Source=StudentDatabase.accdb")
    Dim objStudentDA As New OleDb.OleDbDataAdapter("Select * From Students", objConnection)
    Dim objStudentCB As New OleDb.OleDbCommandBuilder(objStudentDA)
    Dim objDataSet As New DataSet()
    Dim objModuleDA As New OleDb.OleDbDataAdapter("Select * From Modules", objConnection)
    Dim objModuleCB As New OleDb.OleDbCommandBuilder(objModuleDA)


    Private Sub frmStudentDatabase_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'Call the sub procedure that will fill the dataset
        'Create (Relationships) and Retrieve all Student_ID numbers

        Retrieve()

    End Sub

    Private Sub cboStudents_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboStudents.SelectedIndexChanged

        FillStudentDetails()
        FillModuleDetails()

    End Sub

    Public Sub Retrieve()

        objDataSet.Clear()

        objStudentDA.FillSchema(objDataSet, SchemaType.Source, "Students")
        objStudentDA.Fill(objDataSet, "Students")

        objModuleDA.FillSchema(objDataSet, SchemaType.Source, "Modules")
        objModuleDA.Fill(objDataSet, "Modules")

        objDataSet.Relations.Clear()
        objDataSet.Relations.Add("Students2Modules", objDataSet.Tables("Students").Columns("Student_ID"), _
                             objDataSet.Tables("Modules").Columns("Student_ID"))

        'Empty combo box
        cboStudents.Items.Clear()

        'Loop through each row, adding the Student_ID to the combobox
        Dim i As Integer, strCurrentID As String
        For i = 1 To objDataSet.Tables("Students").Rows.Count
            strCurrentID = objDataSet.Tables("Students").Rows(i - 1).Item("Student_ID")
            cboStudents.Items.Add(strCurrentID)
        Next
        'Select first item in the list
        cboStudents.SelectedIndex = 0

        FillStudentDetails()
        FillModuleDetails()

    End Sub

    Public Sub FillStudentDetails()
        Dim objRow As DataRow
        objRow = objDataSet.Tables("Students").Rows.Find(cboStudents.SelectedItem)
        txtStudentID.Text = objRow.Item("Student_ID")
        txtStudentName.Text = objRow.Item("Student_Name")
        txtStudentAddress.Text = objRow.Item("Student_Address")
    End Sub

    Public Sub FillModuleDetails()
        Dim objStudent As DataRow, objModule As DataRow
        Dim strModuleEntry As String

        'Clear any existing modules
        lstModules.Items.Clear()

        'Find the current student record
        objStudent = objDataSet.Tables("Students").Rows.Find(cboStudents.SelectedItem.ToString)
        For Each objModule In objStudent.GetChildRows("Students2Modules")
            strModuleEntry = objModule.Item("Module_ID") & "," & objModule.Item("Module_Name") &
            "," & objModule.Item("Module_Desc")
            lstModules.Items.Add(strModuleEntry)
            DataGridView1.DataSource = objDataSet.Tables("Modules")
        Next
    End Sub

    Private Sub btnNew_Click(sender As Object, e As EventArgs) Handles btnNew.Click
        Dim objRow As DataRow
        Dim objRowModules As DataRow
        Dim RowIndex As Integer
        objRow = objDataSet.Tables("Students").NewRow

        objRow.Item("Student_Name") = InputBox("Please Enter Student Name:")
        objRow.Item("Student_Address") = InputBox("Please Enter Student Address:")

        'Add data row to table
        objDataSet.Tables("Students").Rows.Add(objRow)

        objStudentDA.Update(objDataSet, "Students")

        'Set up the module data row object
        objRowModules = objDataSet.Tables("Modules").NewRow

        'Find the number of the last row
        RowIndex = objDataSet.Tables("Students").Rows.Count - 1

        'Tell vb.net to get the Student_ID value from the last row
        objRowModules.Item("Student_ID") = objDataSet.Tables("Students").Rows(RowIndex)("Student_ID")
        objRowModules.Item("Module_Name") = InputBox("Please Enter Module Name:")
        objRowModules.Item("Module_Desc") = InputBox("Please Enter Module Description:")

        'Add to the DB
        objDataSet.Tables("Modules").Rows.Add(objRowModules)

        'Update the data adapter
        objModuleDA.Update(objDataSet, "Modules")

        MessageBox.Show("New record saved", "Saved")
        Retrieve()

    End Sub

    Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
        'Code to modify an existing record
        Dim objRowCurrent As DataRow
        objRowCurrent = objDataSet.Tables("Students").Rows.Find(cboStudents.SelectedItem.ToString)

        'We cannot modify the ID as it is an AutoNumber
        objRowCurrent("Student_Name") = txtStudentName.Text
        objRowCurrent("Student_Address") = txtStudentAddress.Text
        objStudentDA.Update(objDataSet, "Students")
        objDataSet.AcceptChanges()

        'Now that we made changes, we need to retrieve the new data
        Retrieve()

    End Sub

    Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
        'Dim i As integer, strCurrentID As String
        Dim StudentName As String
        Dim StudentFound As Boolean = False
        StudentName = InputBox("Enter a Name Please!", "Search")
        For i As Integer = 0 To (objDataSet.Tables("Students").Rows.Count - 1)
            If CStr(objDataSet.Tables("Students").Rows(i)("Student_Name")) = StudentName Then
                StudentFound = True
                cboStudents.SelectedIndex = i
            FillStudentDetails()
            FillModuleDetails()
        End If
    Next
End Sub

Private Sub btnNext_Click(sender As Object, e As EventArgs) Handles btnNext.Click
    Dim rowIndex As Integer = cboStudents.SelectedItem - 1
    If (rowIndex < objDataSet.Tables("Students").Rows.Count - 1) Then
        rowIndex = rowIndex + 1
        cboStudents.SelectedIndex = rowIndex
        FillStudentDetails()
        FillModuleDetails()
    Else
        MessageBox.Show("No more records to display", "End")
    End If
End Sub

Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
    Dim objRow As DataRow

    objRow = objDataSet.Tables("Students").Rows.Find(txtStudentID.Text)
    'Remember that all related child rows will be deleted! So no need to 
    'set up a new datarow for the Pet table
    objRow.Delete()
    objStudentDA.Update(objDataSet, "Students")

    Retrieve()

End Sub

Private Sub btnPrevious_Click(sender As Object, e As EventArgs) Handles btnPrevious.Click
    Dim rowIndex As Integer = cboStudents.SelectedItem - 1
    If (rowIndex > 0) Then
        rowIndex = rowIndex - 1
        cboStudents.SelectedIndex = rowIndex
        FillStudentDetails()
        FillModuleDetails()
    Else
        MessageBox.Show("No more records to display", "Start")
    End If
End Sub


End Class

1 个答案:

答案 0 :(得分:0)

您尝试删除包含子记录的记录。由于这些引用,您的删除失败。原因是学生有模块。在删除之前,您需要删除对学生的引用。