Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

SqlDataSource.Deleting Event

Note: This event is new in the .NET Framework version 2.0.

Occurs before a delete operation.

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

public event SqlDataSourceCommandEventHandler Deleting
/** @event */
public void add_Deleting (SqlDataSourceCommandEventHandler value)

/** @event */
public void remove_Deleting (SqlDataSourceCommandEventHandler value)

JScript supports the use of events, but not the declaration of new ones.

Handle the Deleting event to perform additional initialization operations that are specific to your application, to validate the values of parameters, or to change the parameter values before the SqlDataSource control performs the delete operation.

The connection to the underlying data source is not yet open when the event handler delegate is called. Therefore, you cannot cancel the operation directly by calling the Cancel method on the DbCommand object that is exposed by the SqlDataSourceCommandEventArgs object. You can, however, cancel the database operation by setting the Cancel property of the SqlDataSourceCommandEventArgs object to true.

For more information about handling events, see Consuming Events.

The following code example demonstrates how to handle the Deleting event that is raised before a Delete operation occurs. Because this example deletes data from the Northwind database, an OnDeleting handler is added to attempt to backup 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";
}
</SCRIPT>

<HTML>
  <BODY>
    <FORM runat="server">
        <asp:SqlDataSource
            id="SqlDataSource1"
            runat="server"
            DataSourceMode="DataSet"
            ConnectionString="<%$ ConnectionStrings:MyNorthwind%>"
            SelectCommand="SELECT * FROM Orders"            
            DeleteCommand="DELETE FROM [Order Details] WHERE OrderID=@OrderID;DELETE FROM Orders WHERE OrderID=@OrderID;"
            OnDeleting="OnRecordDeleting"
            OnDeleted="OnRecordDeleted">
        </asp:SqlDataSource>
        <br />
       <asp:CheckBox 
         id="CheckBox1" 
         runat="server"
         autopostback="true"
         text="Check To Delete Data" />
        <br />
        <br />

        <asp:GridView
            id="GridView1"
            runat="server"
            AutoGenerateColumns="False"
            DataKeyNames="OrderID"
            AutoGenerateDeleteButton="True"
            AllowPaging="True"
            PageSize="20"
            DataSourceID="SqlDataSource1">
            <Columns>
                <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" />
            </Columns>
        </asp:GridView>

        <asp:Label
            id="Label1"
            runat="server">
        </asp:Label>

    </FORM>
  </BODY>
</HTML>

<%@Page  Language="VJ#" %>
<%@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)
    {
        // Because this example actually deletes data from the Northwind
        // database, provide a way for users to recover any deleted records.
        SqlConnection cxn = new SqlConnection(
            "Data Source=localhost;Integrated Security=SSPI;" 
            + "Initial Catalog=Northwind;Connect Timeout=15");

        try {
            cxn.Open();
            SqlCommand backup = new SqlCommand();
            backup.set_Connection (cxn);
            backup.set_CommandText(
                " USE master " + " EXEC sp_addumpdevice 'disk','Northwind_1'," 
                + "'c:\\temp\\Northwind_1.dat'" 
                + " BACKUP DATABASE Northwind TO Northwind_1");
            backup.ExecuteNonQuery();
        }
        catch (SqlException se) {
            // Handle an exception thrown if the device already exists.
            Label1.set_Text("An error occurred while backing up your " 
                + "database. Please check the SQL logs.");
        }
        finally {
            // Always release the connection.
            cxn.Dispose();
        }
        Label1.set_Text("A record has been deleted. To recover your data, " 
            + "restore the database from the " 
            + "database backup named Northwind_1.dat located on the database " 
            + "server in c:\\temp.");
    } //OnRecordDeleting
</SCRIPT>

<HTML>
    <BODY>
        <FORM runat="server">

            <asp:SqlDataSource
                id="SqlDataSource1"
                runat="server"
                DataSourceMode="DataSet"
                ConnectionString="Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Northwind;"
                SelectCommand="SELECT * FROM Orders"
                DeleteCommand="DELETE FROM [Order Details] WHERE OrderID=@OrderID;DELETE FROM Orders WHERE OrderID=@OrderID;"
                OnDeleting="OnRecordDeleting">
            </asp:SqlDataSource>

            <asp:GridView
                id="GridView1"
                runat="server"
                AutoGenerateColumns="False"
                DataKeyNames="OrderID"
                AutoGenerateDeleteButton="True"
                AllowPaging="True"
                PageSize="20"
                DataSourceID="SqlDataSource1">
                <Columns>
                    <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" />
                </Columns>
            </asp:GridView>

            <asp:Label
                id="Label1"
                runat="server">
            </asp:Label>

        </FORM>
    </BODY>
</HTML>

Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft