how to delete in lisview base on selected items help pls

this is my sql code for delete

Try
            If identifier = Nothing Then
                MessageBoxEx.EnableGlass = False
                MessageBoxEx.Show("Please Choose an Item to Delete", "Specialized Training", MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Exclamation)
            Else
                Select Case MessageBox.Show("Are you sure you want to delete?", "Specialized Training", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)
                    Case DialogResult.Yes
                        ConnectDatabase()
                        Dim SqlQuery As String = "Delete from specializedtraining Where NR='" & lvList.SelectedItems(0).Text & "'"
                        Dim SqlCommand As New MySqlCommand


                        With SqlCommand
                            .CommandText = SqlQuery
                            .Connection = Conn.conn
                            .ExecuteNonQuery()
                        End With
                        LoadRecords()
                        MessageBoxEx.EnableGlass = False
                        MessageBoxEx.Show("Item Successfully Deleted", "Specialized Training", MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information)
                        SqlCommand.Dispose()
                        DisconnectDatabase()


                End Select
            End If
            Try
            Catch ex As MySqlException
                MsgBox(ex.Message, MsgBoxStyle.Critical)
            End Try
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical)
        End Try
        identifier = Nothing

Dani AI

Generated

A couple of practical notes that pick up on ’s original question and ’s UI tip: deleting multiple items is really two tasks — remove the corresponding rows in MySQL, then update the ListView (or simply reload it). Make sure the ListView’s MultiSelect (or CheckBoxes + CheckedItems) is enabled so you can actually select more than one entry.

Recommended pattern: gather the primary keys from the selected items, open the DB connection once, use a parameterized command (or a single IN-style statement) and run the delete(s) inside a transaction. Parameterized queries avoid SQL injection and type issues; a single connection/transaction is faster and safer than opening/closing per item. After the DB confirms deletion, refresh the UI (call your existing LoadRecords() or rebuild the list) rather than removing items one-by-one in the loop to avoid sync problems.

Example — reuse a prepared parameter and execute for each selected key:

' prepare once, run for each selected row
Using conn As New MySqlConnection(connString)
    conn.Open()
    Using tx = conn.BeginTransaction()
        Using cmd As New MySqlCommand("DELETE FROM specializedtraining WHERE NR = @nr", conn, tx)
            cmd.Parameters.Add(New MySqlParameter("@nr", MySqlDbType.VarChar))
            For Each lvi As ListViewItem In lvList.SelectedItems
                cmd.Parameters("@nr").Value = lvi.SubItems(0).Text  ' adjust subitem index if needed
                cmd.ExecuteNonQuery()
            Next
        End Using
        tx.Commit()
    End Using
End Using

LoadRecords()  ' refresh the ListView from the DB

Alternative (faster for many items): build a DELETE ... WHERE NR IN (@p0,@p1,...) with one command and add one parameter per selected key.

Troubleshooting: if nothing deletes, confirm you’re reading the correct subitem index and that NR’s DB type matches the parameter type; watch for foreign-key constraints; check ExecuteNonQuery's return value for affected rows; catch MySqlException to see DB errors; for large batches do work off the UI thread and then refresh on completion.

Recommended Answers

All 3 Replies

Perhaps if you asked a clearer question and told us what your code is supposed to do we might be able to help.

how to delete multi items in listview?

For Each item As ListViewItem In ListView1.SelectedItems
    item.Remove()
Next
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.