Sunday 5 June 2016

How to: Delete records from SQL table in VB.Net

How to: Delete records from SQL table in VB.Net 




Code to delete a record from SQL table using VB.Net is as follows:


Note-
1: Double Click the delete button and copy the following code
2:And also change the server in connection string and the database also

Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
        Try
            con.ConnectionString = "Server=.\SQLEXPRESS;Database=Employee Details;Trusted_Connection=True"
            con.Open()
            cmd.Connection = con
            cmd.CommandText = "Delete From emp_det where emp_id like @emp_id"

            cmd.Parameters.Add(New SqlParameter("@emp_id", TextBox1.Text))
            cmd.ExecuteNonQuery()
            If MessageBox.Show("Do you really want to Delete this Record?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = DialogResult.No Then

                MsgBox("Operation Cancelled")

                Exit Sub

            End If
        Catch ex As Exception
            MessageBox.Show("Error while deleting record on table..." & ex.Message, "Delete Records")

        Finally

            con.Close()

        End Try

    End Sub






No comments:

Post a Comment