In this article I will explain with an example, how to Delete data from Database in ASP.Net using C# and VB.Net.
ADO.Net will be used to perform Delete operation in ASP.Net.
I have made use of the following table Customers with the schema as follows.
Delete data from Database in ASP.Net using C# and VB.Net
I have already inserted few records in the table.
Delete data from Database in ASP.Net using C# and VB.Net
Note: You can download the database table SQL by clicking the download link below.
          Download SQL file
HTML Markup
The HTML Markup consists of:
TextBox – For entering Id.
Button – Submitting the Form.
The Button has been assigned OnClick event handler.
<table border="0" cellpadding="0" cellspacing="0">
        <td style="width: 60px">Id<br />
            <asp:TextBox ID="txtId" runat="server" Width="50px" />
        <td style="width: 200px">
            <br />
            <asp:Button Text="Delete" runat="server" OnClick="OnSubmit" />
You will need to import the following namespaces.
using System.Configuration;
using System.Data.SqlClient;
Imports System.Configuration
Imports System.Data.SqlClient
Deleting the record from Database in ASP.Net
When the Delete Button is clicked, following event handler is executed.
Inside this event handler, the CustomerId is fetched from the TextBox and the record is deleted from the SQL Server database table using ADO.Net.
Finally, appropriate message is displayed in JavaScript Alert Message Box using RegisterStartupScript method.
protected void OnSubmit(object sender, EventArgs e)
    string query = "DELETE FROM Customers WHERE CustomerId=@CustomerId";
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
        using (SqlCommand cmd = new SqlCommand(query))
            cmd.Parameters.AddWithValue("@CustomerId", txtId.Text);
            cmd.Connection = con;
            int i = cmd.ExecuteNonQuery();
            if (i > 0)
                ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Customer record deleted.');", true);
                ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Customer not found.');", true);
Protected Sub OnSubmit(ByVal sender As Object, ByVal e As EventArgs)
    Dim query As String = "DELETE FROM Customers WHERE CustomerId=@CustomerId"
    Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Using con As SqlConnection = New SqlConnection(constr)
        Using cmd As SqlCommand = New SqlCommand(query)
            cmd.Parameters.AddWithValue("@CustomerId", txtId.Text)
            cmd.Connection = con
            Dim i As Integer = cmd.ExecuteNonQuery()
            If i > 0 Then
                ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('Customer record deleted.');", True)
                ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('Customer not found.');", True)
            End If
        End Using
    End Using
End Sub
Delete data from Database in ASP.Net using C# and VB.Net