Первый метод - это больше кода, но второй метод, который, я думаю, ближе к тому, что было предложено в комментариях @jmcilhinney, может создать проблему. Я думаю, что DataAdapte
r требует, чтобы первичный ключ был частью выбора. Если FullName
не является первичным ключом таблицы судей, этот метод может не работать.
Вы можете сделать sh, чтобы сделать dt
(Datatable
) переменной уровня формы, чтобы вы не не нужно извлекать и приводить его из свойства .DataSource
. Затем он будет доступен как для метода, в котором вы заполняете комбо, так и для метода, в котором вы удаляете судью. Обязательно удалите локальные переменные, если вы сделаете это.
Private Sub FillJudgesCombo()
Dim dt As New DataTable
Using cn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\athan\Documents\PAGEANT.accdb"),
cmd As New OleDbCommand("Select FullName From Judges")
cn.Open()
dt.Load(cmd.ExecuteReader)
End Using
cmbJudges.DisplayMember = "FullName"
cmbJudges.DataSource = dt
End Sub
Private Sub DeleteJudge1()
'I stored this in a local variable because we are using it more tha once in this method
Dim NameToDelete = cmbJudges.Text
'To update the database
Using cons As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\athan\Documents\PAGEANT.accdb"),
cmd As New OleDbCommand("DELETE FROM Judges Where Fullname = @FullName", cons)
'had to guess at the datatype and field size - check your database
'The Add method is much preferred to the .AddWithValue
cmd.Parameters.Add("@FullName", OleDbType.VarChar, 200).Value = NameToDelete
cons.Open()
cmd.ExecuteNonQuery()
End Using
'To update the combo box by editing DataTable
Dim dt = DirectCast(ComboBox1.DataSource, DataTable)
'This line uses and interpolated string indicated by the $ preceding the string.
'This allows to insearch a variable in place in the string surronded by braces.
Dim dr = dt.Select($"FullName = {NameToDelete}")
dr(0).Delete()
dt.AcceptChanges()
ResetTextBoxesAndCombo()
MessageBox.Show("RECORD HAS BEEN DELETED", "DELETE", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub
Private Sub ResetTextBoxesAndCombo()
'Offloaded this because it really has nothing to do with deleting a judge.
txtFullNameJudge.Text = Nothing
txtContactJudge.Text = Nothing
txtUserNameJudge.Text = Nothing
txtPasswordJudge.Text = Nothing
cmbJudges.SelectedIndex = -1
End Sub
Private Sub DeleteJudge2()
Dim NameToDelete = cmdJudges.Text
Dim dt = DirectCast(ComboBox1.DataSource, DataTable)
'The Select method has no idea how many rows are returned
'so it returns an array of DataRows
Dim dr = dt.Select($"FullName = {NameToDelete}")
'We are only interested in the first item in the array.
'The array should only have one element.
dr(0).Delete()
Using con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\athan\Documents\PAGEANT.accdb"),
da As New OleDbDataAdapter("Select FullName From PAGEANT", con)
Dim cmdBuild As New OleDbCommandBuilder(da)
da.Update(dt)
End Using
ResetTextBoxesAndCombo()
MessageBox.Show("RECORD HAS BEEN DELETED", "DELETE", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub