title: Deleting a row from MySQL in ListView in Visual Studio 2013
body: <p>I'm almost done with a trial program where I add, edit and delete stuff from my MySQL database. </p>
  
  <p>But I can't seem to make the delete button to work.</p>
  
  <p><strong>Here's my code for the Delete Button:</strong></p>
  
  <pre><code>If IDNo = Nothing Then
      MsgBox("Please choose an item to delete.", MsgBoxStyle.Exclamation)
  Else
      Dim sqlQuery As String = "DELETE FROM tbl_adbms_test WHERE IDNo='" &amp; IDNo &amp; "'"
      Dim sqlCommand As New MySqlCommand
  
      With sqlCommand
          .CommandText = sqlQuery
          .Connection = sConnection
          .ExecuteNonQuery()
      End With
  
      MsgBox("Successfully deleted an item.", MsgBoxStyle.Information)
  
      Me.LoadPeople()
  End If
  </code></pre>
  
  <p><strong>The ERROR</strong></p>
  
  <p><a href="http://stivigan.us.to/images/delete_error.jpg" rel="nofollow">http://stivigan.us.to/images/delete_error.jpg</a></p>
  
  <p><strong>And here's the rest of my Main Form.</strong></p>
  
  <pre><code>Imports MySql.Data.MySqlClient
  </code></pre>
  
  <p>Public Class frm_main
      Public sConnection As New MySqlConnection
      Public IDNo As Integer</p>
  
  <pre><code>Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
      If sConnection.State = ConnectionState.Closed Then
          sConnection.ConnectionString = "SERVER = localhost; USERID = root; PASSWORD = loadedro; DATABASE = adbms_test_db"
      End If
  
      LoadPeople()
  End Sub
  
  Public Sub LoadPeople()
      Dim sqlQuery As String = "SELECT * FROM tbl_adbms_test"
      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
  
      list_view_people.Items.Clear()
  
      For i = 0 To TABLE.Rows.Count - 1
          With list_view_people
              .Items.Add(TABLE.Rows(i)("IDNo"))
              With .Items(.Items.Count - 1).SubItems
                  .Add(TABLE.Rows(i)("LastName"))
                  .Add(TABLE.Rows(i)("FirstName"))
              End With
          End With
      Next
  End Sub
  
  Private Sub btn_save_Click(sender As Object, e As EventArgs) Handles btn_add.Click
      frm_add.ShowDialog()
  End Sub
  
  Private Sub btn_modify_Click(sender As Object, e As EventArgs) Handles btn_modify.Click
      If IDNo = Nothing Then
          MsgBox("Please choose a record to modify.", MsgBoxStyle.Exclamation)
      Else
          Dim sqlQuery As String = "SELECT LastName, FirstName FROM tbl_adbms_test WHERE IDNo='" &amp; list_view_people.SelectedItems(0).Text &amp; "'"
          Dim sqlAdapter As New MySqlDataAdapter
          Dim sqlCommand As New MySqlCommand
          Dim sqlTable As New DataTable
  
          With sqlCommand
              .CommandText = sqlQuery
              .Connection = sConnection
          End With
  
          With sqlAdapter
              .SelectCommand = sqlCommand
              .Fill(sqlTable)
          End With
  
          frm_modify.IDNo = list_view_people.SelectedItems(0).Text
          frm_modify.LastName = sqlTable.Rows(0)("LastName")
          frm_modify.FirstName = sqlTable.Rows(0)("FirstName")
          frm_modify.ShowDialog()
      End If
  End Sub
  
  Private Sub list_view_people_MouseClick(sender As Object, e As MouseEventArgs) Handles list_view_people.MouseClick
      IDNo = list_view_people.SelectedItems(0).Text
  End Sub
  
  Private Sub btn_delete_Click(sender As Object, e As EventArgs) Handles btn_delete.Click
      If IDNo = Nothing Then
          MsgBox("Please choose an item to delete.", MsgBoxStyle.Exclamation)
      Else
          Dim sqlQuery As String = "DELETE FROM tbl_adbms_test WHERE IDNo='" &amp; IDNo &amp; "'"
          Dim sqlCommand As New MySqlCommand
  
          With sqlCommand
              .CommandText = sqlQuery
              .Connection = sConnection
              .ExecuteNonQuery()
          End With
  
          MsgBox("Successfully deleted an item.", MsgBoxStyle.Information)
  
          Me.LoadPeople()
      End If
  End Sub
  </code></pre>
  
  <p>End Class</p>
  
  <p><strong>Add Form</strong></p>
  
  <pre><code>Public Class frm_add
  Public sConnection As New MySqlConnection
  Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
      If sConnection.State = ConnectionState.Closed Then
          sConnection.ConnectionString = "SERVER = localhost; USERID = root; PASSWORD = loadedro; DATABASE = adbms_test_db"
      End If
  End Sub
  
  Private Sub btn_save_Click(sender As Object, e As EventArgs) Handles btn_save.Click
      If sConnection.State = ConnectionState.Closed Then
          sConnection.ConnectionString = "SERVER = localhost; USERID = root; PASSWORD = loadedro; DATABASE = adbms_test_db"
          sConnection.Open()
      End If
  
      Dim sqlQuery As String = "INSERT INTO tbl_adbms_test(IDNo,LastName,FirstName) VALUES(NULL,'" &amp; txt_last_name.Text &amp; "','" &amp; txt_first_name.Text &amp; "')"
      Dim sqlCommand As New MySqlCommand
  
      With sqlCommand
          .CommandText = sqlQuery
          .Connection = sConnection
          .ExecuteNonQuery()
      End With
  
      MsgBox("The data was saved.", MsgBoxStyle.Information)
      Dispose()
      Close()
      frm_main.LoadPeople()
  End Sub
  </code></pre>
  
  <p>End Class</p>
  
  <p><strong>Edit Form</strong></p>
  
  <pre><code>Imports MySql.Data.MySqlClient
  </code></pre>
  
  <p>Public Class frm_modify
      Friend IDNo As Integer
      Friend LastName As String
      Friend FirstName As String</p>
  
  <pre><code>Public sConnection As New MySqlConnection
  Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
      txt_last_name.Text = LastName
      txt_first_name.Text = FirstName
  End Sub
  
  Private Sub btn_update_Click(sender As Object, e As EventArgs) Handles btn_update.Click
      If sConnection.State = ConnectionState.Closed Then
          sConnection.ConnectionString = "SERVER = localhost; USERID = root; PASSWORD = loadedro; DATABASE = adbms_test_db"
          sConnection.Open()
      End If
  
      Dim sqlQuery As String = "UPDATE tbl_adbms_test SET LastName='" &amp; txt_last_name.Text &amp; "', FirstName='" &amp; txt_first_name.Text &amp; "' WHERE IDNo='" &amp; IDNo &amp; "'"
      Dim sqlCommand As New MySqlCommand
  
      With sqlCommand
          .CommandText = sqlQuery
          .Connection = sConnection
          .ExecuteNonQuery()
      End With
  
      MsgBox("Record updated successfully.", MsgBoxStyle.Information)
      Dispose()
      Close()
      frm_main.LoadPeople()
  End Sub
  </code></pre>
  
  <p>End Class</p>
  
  <p><strong>Thanks in advance. :)</strong></p>