Example : How to Delete/Remove the Data/Record from the SQL Server Database

How to Delete Single  Selected  Record from a database

Private Sub Button1_Click(sender As Object, e As EventArgs)
   Dim k As Integer  = MsgBox("DO YOU WANT TO DELETE THE SELECTED CATEGORY RECORD ", MsgBoxStyle.YesNo)
   If k = 6 Then
     Try
        cn.Open()
        cm.CommandText = "DELETE * FROM NewCenter Where CATEGORY='"
            + CmbCat.Text + "' AND CENTERCODE='" + CmbCc.Text + "' "
        Dim i As Integer = cm.ExecuteNonQuery
        If i = 0 Then
          MsgBox("RECORD NOT FOUND")          
          CmbCat.Text = ""
          CmbCat.Focus()
          Exit Sub
       End If
       cn.Close()
       MsgBox(i.ToString + " RECORD DELETED SUCCESSFULLY ")
     Catch ex As Exception
       MsgBox(ex.Message)
     Finally
       cn.Close()
     End Try
  End if
End Sub
How to Delete the confirmed Record from a database
Private Sub Button1_Click(sender As Object, e As EventArgs) 
 Dim p As String = ""
 Dim u As String = ""

 cn.Open()
 cm.CommandText = "select category,centercode from newcenter"
 rdr = cm.ExecuteReader(CommandBehavior.SequentialAccess)

 while (rdr.Read())           
    u = rdr("category").ToString
    p = rdr("centercode").ToString

    If CmbCat.Text = u And CmbCc.Text = p Then
       cn.Close()
       Dim k As Integer = MsgBox("DO YOU WANT TO DELETE THE FOUND RECORD",
           MsgBoxStyle.YesNo)
       If k = 6 Then
          Try
            cn.Open()
               cm.CommandText = "DELETE * FROM NewCenter Where  "
                  CENTERCODE='" + CmbCc.Text + "' AND CATEGORY='" +   
               CmbCat.Text +"'"
               Dim i As Integer = cm.ExecuteNonQuery
            cn.Close()
               MsgBox(i.ToString + " RECORD DELETED SUCCESSFULLY ")
         Catch ex As Exception
            MsgBox(ex.Message)
         Finally
            cn.Close()
         End Try
       End If
    End If
 End While
End Sub

How to Delete All Records from a database

Private Sub Button1_Click(sender As Object, e As EventArgs)
   Dim k As Integer  = MsgBox("DO YOU WANT TO DELETE THE SELECTED CATEGORY RECORD ", MsgBoxStyle.YesNo)"
   If k = 6 Then
     Try
        cn.Open()       
        'cm.CommandText = "DELETE * FROM NewCenter"   
        Dim i As Integer = cm.ExecuteNonQuery
        If i = 0 Then
          MsgBox("RECORD NOT FOUND")          
          CmbCat.Text = ""
          CmbCat.Focus()
          Exit Sub
       End If
       cn.Close()
       MsgBox(i.ToString + " RECORD DELETED SUCCESSFULLY ")
     Catch ex As Exception
       MsgBox(ex.Message)
     Finally
       cn.Close()
     End Try
 End Sub

How to Delete selected Record from a List view on pressing Delete Key

Private Sub ListView1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles listView1.KeyDown
        If e.KeyCode = Keys.Delete Then
            Dim SelectedItemInList As New ListViewItem
            SelectedItemInList = ListView1.SelectedItems(0)
            SelectedItemInList.Remove()
        End If
End Sub

Loading

Categories: VB .Net Codes

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.