SqlDataSourceView.DeleteCommand Property


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Gets or sets the SQL string that the SqlDataSourceView uses to delete data from the underlying database.

Namespace:   System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)

public string DeleteCommand { get; set; }

Property Value

Type: System.String

An SQL string that the SqlDataSourceView uses to delete data.

Because different database products use different varieties of SQL, the syntax of the SQL string depends on the current ADO.NET provider being used, which is identified by the ProviderName property.

If the SQL string is a parameterized query or command, the placeholder of the parameter also depends on the ADO.NET provider being used. For example, if the provider is the System.Data.SqlClient, which is the default provider for the SqlDataSource class, the placeholder of the parameter is '@parameterName'. However, if the provider is set to the System.Data.Odbc or System.Data.OleDb, the placeholder of the parameter is '?'. For more information on parameterized SQL queries and commands, see Using Parameters with the SqlDataSource Control.

The DeleteCommand can be an SQL string or the name of a stored procedure, if the underlying database supports stored procedures.

The value of the DeleteCommand property is stored in view state.

The following code example demonstrates how to set the DeleteCommand text to delete an order from the Northwind database Orders table. Data is retrieved from the Orders table and displayed in a GridView control. The GridView renders a Delete button automatically when the AutoGenerateDeleteButton property is set to true and automatically populates the DeleteParameters collection and calls the Delete method when the Delete button is clicked. Finally, because this example deletes data, an event handler is added to attempt to back up the database to disk before the delete operation is performed.

<%@Page  Language="C#" %>
<%@Import Namespace="System.Data.SqlClient" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
private void OnRecordDeleting(Object source, SqlDataSourceCommandEventArgs e) {    
    // Cancel the delete operation if the checkbox is not checked.
    if (! CheckBox1.Checked) {
        e.Cancel = true;
        Label1.Text = "The command was cancelled because the CheckBox was not checked.";

private void OnRecordDeleted(object source, SqlDataSourceStatusEventArgs e) {
    Label1.Text = e.AffectedRows + " row(s) were deleted";

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>ASP.NET Example</title>
    <form id="form1" runat="server">
            ConnectionString="<%$ ConnectionStrings:MyNorthwind%>"
            SelectCommand="SELECT * FROM Orders"            
            DeleteCommand="DELETE FROM [Order Details] WHERE OrderID=@OrderID;DELETE FROM Orders WHERE OrderID=@OrderID;"
        <br />
         text="Check To Delete Data" />
        <br />
        <br />

                <asp:BoundField HeaderText="Order ID" DataField="OrderID" />
                <asp:BoundField HeaderText="Customer" DataField="CustomerID" />
                <asp:BoundField HeaderText="Order Placed" DataField="OrderDate" />
                <asp:BoundField HeaderText="Order Shipped" DataField="ShippedDate" />



.NET Framework
Available since 2.0
Return to top