2017-04-03 49 views
0

如果刪除記錄存在問題,則會繼續發生此異常。由於'模塊'表包含相關記錄,因此無法刪除或更改記錄

類型「System.Data.OleDb.OleDbException」未處理的異常出現在system.data.dll

附加信息:記錄不能刪除或更改,因爲表「模塊」包含相關記錄。

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 
+4

什麼令消息混淆?如果記錄有引用它的子記錄,則不能先刪除「父」。此外Visual Studio與問題無關 – Plutonix

+3

請嘗試只發布相關代碼到您的問題。 [mcve]是最好的選擇 – Steve

回答

0

您嘗試刪除具有子記錄的記錄。由於這些引用您的刪除失敗。原因是學生有模塊。您需要刪除對學生的引用,然後才能將其刪除。