title: Delete ListView items in VB.NET
body: <p>How do i delete items in the <code>ListView</code> at the same time in my database? I have this code but i don't know what's wrong,but it functions well and it has no errors but it seems that it's not deleting any items?</p>
  
  <p>here's my code:</p>
  
  <pre><code>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&lt;&gt;'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='" &amp; LvPeople.SelectedItems(0).Text &amp; "'"
  
              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
  </code></pre>