ObjectDataSource.DeleteParameters Property
Assembly: System.Web (in system.web.dll)
/** @property */ public ParameterCollection get_DeleteParameters ()
public function get DeleteParameters () : ParameterCollection
Not applicable.
Property Value
A ParameterCollection that contains the parameters used by the DeleteMethod method.The names and types of the parameters that are contained in the DeleteParameters collection must match the names and types of the parameters in the DeleteMethod method signature. The parameter names are affected by the OldValuesParameterFormatString property and are case-sensitive. The parameters in the collection depend on the data that is in the data-bound control, the parameters that are specified declaratively, and the parameters that are added programmatically. For more information, see "Parameter Merging" in Delete and Parameters with the ObjectDataSource Control.
Note: |
|---|
| If you are not familiar with the Data Access features in ASP.NET 2.0, you should read some of these topics before continuing: |
The DeleteParameters property retrieves the DeleteParameters property that is contained by the ObjectDataSourceView object that is associated with the ObjectDataSource control.
For more information about parameter merging, object lifetime, and method resolution, see DeleteMethod.
This section contains two code examples. The first code example demonstrates how to use an ObjectDataSource object with a business object and a GridView control to delete data. The second code example shows the EmployeeLogic class that is used in the first code example.
The following code example demonstrates how to use the DeleteParameters property of the ObjectDataSource control with a business object and a GridView control to delete data. Initially, the GridView control displays a set of all employees, using the method that is specified by the SelectMethod property to retrieve the data from the EmployeeLogic object. Because the AutoGenerateDeleteButton property is set to true, the GridView control automatically displays a Delete button.
If you click the Delete button, the Deleting event is called with two parameters. One parameter, EmpID, comes from the DataGrid control, and is based on the DataKeyNames property. The other parameter comes from the declarative DeleteParameters property. The EmployeeLogic class overloads the DeleteEmployee method. One overload takes a NorthwindEmployee parameter, and the other takes an Int32 parameter named anID. In the Deleting event, the entries in the InputParameters property are adjusted to include only one parameter named anID with the value from original_EmpID. When the event handler exists, the matching DeleteEmployee method is called.
The following code example shows the Web page that displays the DataGrid control and handles the Deleting event.
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.JSL" Assembly="Samples.AspNet.JSL" %>
<%@ Import namespace="Samples.AspNet.JSL" %>
<%@ 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 NorthwindEmployeeDeleting(Object source,
ObjectDataSourceMethodEventArgs e)
throws NorthwindDataException, System.Data.SqlClient.SqlException
{
// The GridView passes the ID of the employee
// to be deleted. However, it is named "EmpID", the name
// of the property that the value is retrieved from
// the NorthwindEmployee object. By default, this
// name is passed as a parameter to the method
// identified by the DeleteMethod property, which
// in this case is named "anID". Because of the
// naming discrepancy between "EmpID" and "anID",
// the ObjectDataSource would throw an exception.
// Therefore, rename the parameter from "EmpID" to
// "anID".
IDictionary paramsFromPage = e.get_InputParameters();
NorthwindEmployee ne = new NorthwindEmployee(
Convert.ToInt32(paramsFromPage.get_Item("EmpID")));
// Remove the old parameter.
paramsFromPage.Clear();
paramsFromPage.Add("ne", ne);
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>ObjectDataSource - VJ# Example</title>
</head>
<body>
<form id="Form1" method="post" runat="server">
<asp:gridview
id="GridView1"
runat="server"
datasourceid="ObjectDataSource1"
autogeneratedeletebutton="true"
datakeynames="EmpID">
</asp:gridview>
<asp:objectdatasource
id="ObjectDataSource1"
runat="server"
selectmethod="GetAllEmployees"
deletemethod="DeleteEmployee"
OnDeleting="NorthwindEmployeeDeleting"
typename="Samples.AspNet.JSL.EmployeeLogic">
<deleteparameters>
<asp:parameter name="anID" type="Int32" />
</deleteparameters>
</asp:objectdatasource>
</form>
</body>
</html>
The following code example shows the EmployeeLogic class that is used in the preceding code example.
package Samples.AspNet.JSL;
import System.*;
import System.Collections.*;
import System.Configuration.*;
import System.Data.*;
import System.Data.SqlClient.*;
import System.Web.UI.*;
import System.Web.UI.WebControls.*;
//
// EmployeeLogic is a stateless business object that encapsulates
// the operations you can perform on a NorthwindEmployee object.
//
public class EmployeeLogic
{
// Returns a collection of NorthwindEmployee objects.
public static ICollection GetAllEmployees()
throws NorthwindDataException, SqlException
{
ArrayList al = new ArrayList();
// Use the SqlDataSource class to wrap the
// ADO.NET code required to query the database.
ConnectionStringSettings cts = ConfigurationManager.get_ConnectionStrings().
get_Item("NorthwindConnection");
SqlDataSource sds = new SqlDataSource(cts.get_ConnectionString(),
"SELECT EmployeeID FROM Employees");
try {
IEnumerable ids = sds.Select(DataSourceSelectArguments.get_Empty());
// Iterate through the Enumeration and create a
// NorthwindEmployee object for each ID.
IEnumerator enumerator = ids.GetEnumerator();
while (enumerator.MoveNext()) {
// The IEnumerable contains DataRowView objects.
DataRowView row = (DataRowView)enumerator.get_Current();
int id = Convert.ToInt32(row.get_Item("EmployeeID"));
NorthwindEmployee nwe = new NorthwindEmployee(id);
// Add the NorthwindEmployee object to the collection.
al.Add(nwe);
}
}
finally {
// If anything strange happens, clean up.
sds.Dispose();
}
return al;
} //GetAllEmployees
public static NorthwindEmployee GetEmployee(int anId)
throws NorthwindDataException, SqlException
{
return new NorthwindEmployee(anId);
} //GetEmployee
public static void DeleteEmployee(NorthwindEmployee ne)
throws NorthwindDataException
{
boolean retval = ne.Delete();
if (!retval) {
throw new NorthwindDataException("Employee delete failed.");
}
// Delete the object in memory.
ne = null;
} //DeleteEmployee
public static void DeleteEmployeeByID(int anId)
throws NorthwindDataException, SqlException
{
NorthwindEmployee tempEmp = new NorthwindEmployee(anId);
DeleteEmployee(tempEmp);
} //DeleteEmployeeByID
} //EmployeeLogic
public class NorthwindEmployee
{
public NorthwindEmployee(int anId)
throws NorthwindDataException, SqlException
{
this.id = (Int32)anId;
ConnectionStringSettings cts = ConfigurationManager.get_ConnectionStrings().
get_Item("NorthwindConnection");
SqlConnection conn = new SqlConnection(cts.get_ConnectionString());
SqlCommand sc = new SqlCommand(" SELECT FirstName,LastName "
+ " FROM Employees WHERE EmployeeID = @empId", conn);
// Add the employee ID parameter and set its value.
sc.get_Parameters().Add(new SqlParameter("@empId", SqlDbType.Int)).
set_Value((Int32)Int32.Parse(Convert.ToString(anId)));
SqlDataReader sdr = null;
try {
conn.Open();
sdr = sc.ExecuteReader();
// This is not a while loop. It only loops once.
if (sdr != null && sdr.Read()) {
// The IEnumerable contains DataRowView objects.
this.firstName = sdr.get_Item("FirstName").ToString();
this.lastName = sdr.get_Item("LastName").ToString();
}
else {
throw new NorthwindDataException("Data not loaded for employee "
+ "id.");
}
}
finally {
try {
if (sdr != null) {
sdr.Close();
}
conn.Close();
}
catch (SqlException exp) {
// Log an event in the Application Event Log.
throw exp;
}
}
} //NorthwindEmployee
private Object id;
/** @property
*/
public Object get_EmpID()
{
return id;
} //get_EmpID
private String lastName;
/** @property
*/
public String get_LastName()
{
return lastName;
} //get_LastName
/** @property
*/
public void set_LastName(String value)
{
lastName = value;
} //set_LastName
private String firstName;
/** @property
*/
public String get_FirstName()
{
return firstName;
} //get_FirstName
/** @property
*/
public void set_FirstName(String value)
{
firstName = value;
} //set_FirstName
public boolean Delete()
{
if (id.Equals(DBNull.Value)) {
// The Employee object is not persisted.
return true;
}
else {
// The Employee object is persisted.
// Use the SqlDataSource control as a convenient wrapper for
// the ADO.NET code needed to delete a record from the database.
ConnectionStringSettings cts = ConfigurationManager.
get_ConnectionStrings().get_Item("NorthwindConnection");
SqlDataSource sds = new SqlDataSource();
try {
sds.set_ConnectionString(cts.get_ConnectionString());
sds.set_DeleteCommand("DELETE FROM Employees WHERE "
+ "EmployeeID=@empID;");
sds.get_DeleteParameters().Add(new Parameter("empID",
TypeCode.Int32, this.id.ToString()));
/* To make this sample fully functional, uncomment these lines
int retval = sds.Delete();
if (retval == 1) { return true; }
return false;
*/
return true;
}
finally {
sds.Dispose();
}
}
} //Delete
} //NorthwindEmployee
public class NorthwindDataException extends System.Exception
{
public NorthwindDataException(String msg)
{
super(msg);
} //NorthwindDataException
} //NorthwindDataException
Note: