ADO.NET - SPLessons

ADO.Net Delete Command

Home > Lesson > Chapter 10
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

ADO.Net Delete Command

ADO.Net Delete Command

shape Description

Delete command is used to delete the data from the database(SQL).

shape Syntax

The following is the syntax for the delete command. [csharp]SqlCommand cmd=new SqlCommand("delete from table_name where column_name=textboxvalue",connection);[/csharp]

shape Examples

Follow the below steps to work with update command.
  • Create a new ASP.Net Web Application.
  • Write the following code in the design form code behind(Example.aspx).
[html] <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication2.WebForm1" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <style type="text/css"> .auto-style1 { width: 100%; } .auto-style2 { font-weight: bold; text-align: right; height: 26px; } .auto-style3 { height: 26px; } </style> </head> <body> <form id="form1" runat="server"> <div> <table class="auto-style1"> <tr> <td class="auto-style2">Name</td> <td class="auto-style3"> <asp:TextBox ID="txtName" runat="server"></asp:TextBox> </td> </tr> <tr> <td>&nbsp;</td> <td> <asp:Button ID="btnDelete" runat="server" style="font-weight: 700" Text="Delete" Width="85px" Height="26px" OnClick="btnInsert_Click" /> </td> </tr> </table> </div> </form> </body> </html> [/html]
  • Write the following C# code in the Code file(Example.aspx.cs)
[csharp]using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Data.SqlClient; namespace WebApplication2 { public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void btnInsert_Click(object sender, EventArgs e) { SqlConnection con = new SqlConnection("Data Source=hppavillion-pc;Initial Catalog=sample;Integrated Security=True"); SqlCommand cmd = new SqlCommand("delete from employee where name='"+txtName.Text+"'", con); con.Open(); cmd.ExecuteNonQuery(); con.Close(); } } } [/csharp]
  • Run the application. Then you will get the following output.
Open the SQl Server and check whether the delete operation is performed or not.