데이터베이스에서 동시에 ListView
에있는 항목을 어떻게 삭제합니까? 나는이 코드를 가지고 있지만, 무엇이 잘못 되었는가를 잘 모른다. 그러나 그것은 잘 작동하고 에러가 없지만 어떤 아이템도 삭제하지 않는 것 같다.VB.NET에서 ListView 항목을 삭제하십시오.
여기 내 코드입니다 :
Imports MySql.Data.MySqlClient
Public Class Form5
Dim id As Integer
Dim con As New MySqlConnection
Private Sub Form5_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim con As New MySqlConnection
If con.State = ConnectionState.Closed Then
con.ConnectionString = "server=localhost;user id=root;database=db;password=root"
con.Open()
End If
LoadPeople()
End Sub
Public Sub LoadPeople()
Dim sConnection As New MySqlConnection
sConnection.ConnectionString = "server=localhost;user id=root;database=db;password=root"
sConnection.Open()
Dim sqlQuery As String = "SELECT * FROM candidate WHERE cfname<>'Select a Candidate' AND candidacy='Filed'"
Dim sqlAdapter As New MySqlDataAdapter
Dim sqlCommand As New MySqlCommand
Dim TABLE As New DataTable
Dim i As Integer
With sqlCommand
.CommandText = sqlQuery
.Connection = sConnection
End With
With sqlAdapter
.SelectCommand = sqlCommand
.Fill(TABLE)
End With
LvPeople.Items.Clear()
For i = 0 To TABLE.Rows.Count - 1
With LvPeople
.Items.Add(TABLE.Rows(i)("idn"))
With .Items(.Items.Count - 1).SubItems
.Add(AddFieldValue(TABLE.Rows(i), ("cpos")))
.Add(AddFieldValue(TABLE.Rows(i), ("cfname")))
.Add(AddFieldValue(TABLE.Rows(i), ("cmname")))
.Add(AddFieldValue(TABLE.Rows(i), ("clname")))
.Add(AddFieldValue(TABLE.Rows(i), ("cparty")))
End With
End With
Next
End Sub
Private Function AddFieldValue(ByVal row As DataRow, ByVal fieldName As String) As String
If Not DBNull.Value.Equals(row.Item(fieldName)) Then
Return CStr(row.Item(fieldName))
Else
Return Nothing
End If
End Function
Private Sub lvPeople_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles LvPeople.MouseClick
id = LvPeople.SelectedItems(0).Selected
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim con As New MySqlConnection
Dim sqlCommand As New MySqlCommand
con.ConnectionString = "server = localhost; user id = root; database = db; password = root"
sqlCommand.Connection = con
con.Open()
If id = Nothing Then
MsgBox("Please choose a record to remove.", MsgBoxStyle.Exclamation)
Else
Dim result1 As DialogResult = MessageBox.Show("Are you sure to delete this entry?", "Delete Entry", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If result1 = DialogResult.Yes Then
Dim sqlQuery As String = "DELETE FROM candidate WHERE cid='" & LvPeople.SelectedItems(0).Text & "'"
With sqlCommand
.CommandText = sqlQuery
.Connection = con
.ExecuteNonQuery()
End With
MsgBox("Record Removed", MsgBoxStyle.Information)
LoadPeople()
id = Nothing
End If
End If
End Sub
End Class
'id'는 정수이므로 실제로는 Nothing 일 수 없습니다. 'LvPeople.SelectedItems (0) .Remove()'를 시도 했습니까? – TyCobb
'LvPeople.SelectedItems (0) .Remove()'가 적용 가능하지 않습니다. '표현식이 값을 생성하지 않습니다.'라고 말합니다. –
뭔가 설정하려고 시도 했습니까? 그것은'Sub'입니다. – TyCobb