Export (0) Print
Expand All

SqlDataSource.Delete Method

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

Performs a delete operation using the DeleteCommand SQL string and any parameters that are in the DeleteParameters collection.

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

public int Delete ()
public int Delete ()
public function Delete () : int

Return Value

A value that represents the number of rows deleted from the underlying database.

Exception typeCondition

InvalidOperationException

The SqlDataSource cannot establish a connection with the underlying data source.

Before the delete operation is performed, the OnDeleting method is called to raise the Deleting event. You can handle this event to examine the values of the parameters and to perform any preprocessing before a delete operation.

After the operation completes, the OnDeleted method is called to raise the Deleted event. You can handle this event to examine any return values and error codes and to perform any post-processing.

The Delete method is provided for programmatic access to the Delete method. If the SqlDataSource control is associated with a data-bound control, the data-bound control automatically calls the Delete method.

The Delete method delegates to the Delete method of the SqlDataSourceView object that is associated with the SqlDataSource control. To perform the operation, the SqlDataSourceView builds a DbCommand object using the DeleteCommand text and any associated DeleteParameters values, and then executes the DbCommand against the underlying database.

The following code example demonstrates how to set the DeleteCommand text to delete an order from the Northwind database. Initially, data is retrieved from the Orders table and displayed in a DropDownList control. You must explicitly declare the DeleteParameters property and call the Delete method when using data-bound controls, such as the DropDownList (unlike other controls, such as GridView and DetailsView, which automatically populate the parameters and call the Delete method on a data source control). In this example, the OnClick event is delegated to the private OnDelete event handler, which explicitly calls the Delete method of the SqlDataSource control.

<%@Page  Language="C#" %>
<!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 OnDelete(Object sender, EventArgs e) {
    SqlDataSource1.Delete();
}
</SCRIPT>

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

            <asp:SqlDataSource
                id="SqlDataSource1"
                runat="server"
                ConnectionString="<%$ ConnectionStrings:MyNorthwind%>"
                SelectCommand="SELECT OrderID FROM Orders"
                DeleteCommand="DELETE FROM [Order Details] WHERE OrderID=@OrderID;DELETE FROM Orders WHERE OrderID=@OrderID;">
                <DeleteParameters>
                    <asp:ControlParameter Name="OrderID" ControlId="DropDownList1" PropertyName="SelectedValue" />
                </DeleteParameters>
            </asp:SqlDataSource>

            <asp:DropDownList
                id="DropDownList1"
                runat="server"
                DataTextField="OrderID"
                DataValueField="OrderID"
                DataSourceID="SqlDataSource1">
            </asp:DropDownList>

            <asp:Button
                id="Button1"
                runat="server"
                Text="Delete Order"
                OnClick="OnDelete">
            </asp:Button>

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

<%@Page  Language="VJ#" %>
<!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 OnDelete(Object sender, System.EventArgs e)
    {
        SqlDataSource1.Delete();
    } //OnDelete
</SCRIPT>

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

            <asp:SqlDataSource
                id="SqlDataSource1"
                runat="server"
                ConnectionString="Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Northwind;"
                SelectCommand="SELECT OrderID FROM Orders"
                DeleteCommand="DELETE FROM [Order Details] WHERE OrderID=@OrderID;DELETE FROM Orders WHERE OrderID=@OrderID;">
                <DeleteParameters>
                    <asp:ControlParameter Name="OrderID" ControlId="DropDownList1" PropertyName="SelectedValue" />
                </DeleteParameters>
            </asp:SqlDataSource>

            <asp:DropDownList
                id="DropDownList1"
                runat="server"
                DataTextField="OrderID"
                DataValueField="OrderID"
                DataSourceID="SqlDataSource1">
            </asp:DropDownList>

            <asp:Button
                id="Button1"
                runat="server"
                Text="Delete Order"
                OnClick="OnDelete">
            </asp:Button>

        </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

Community Additions

ADD
Show:
© 2014 Microsoft