SqlDataSourceView.OnDeleting Method
.NET Framework 3.0
Raises the Deleting event before the SqlDataSource control attempts a delete operation.
Namespace: System.Web.UI.WebControls
Assembly: System.Web (in system.web.dll)
Assembly: System.Web (in system.web.dll)
protected void OnDeleting ( SqlDataSourceCommandEventArgs e )
protected function OnDeleting ( e : SqlDataSourceCommandEventArgs )
Not applicable.
Parameters
- e
A SqlDataSourceCommandEventArgs that contains the event data.
Raising an event invokes the event handler through a delegate. For more information, see Consuming Events.
The OnDeleting method also allows derived classes to handle the event without attaching a delegate. This is the preferred technique for handling the event in a derived class.
Notes to Inheritors: When overriding the OnDeleting method in a derived class, be sure to call the OnDeleting method for the base class so that registered delegates receive the event.The following code example demonstrates how to handle the Deleting event, which is raised before a Delete operation occurs. Because this example deletes data from the Northwind database, an OnDeleting handler is added to attempt to back up the database to disk before the delete is performed.
<%@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 xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>ASP.NET Example</title>
</head>
<body>
<form id="form1" 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>
Community Additions
ADD
Show: