ObjectDataSource::InsertMethod 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 name of the method or function that the ObjectDataSource control invokes to insert data.

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

property String^ InsertMethod {
	String^ get();
	void set(String^ value);

Property Value

Type: System::String^

A string that represents the name of the method or function that the ObjectDataSource uses to insert data. The default is an empty string ("").

The business object is assumed to insert data one record at a time, rather than in a batch.

The InsertMethod property delegates to the InsertMethod property of the ObjectDataSourceView object that is associated with the ObjectDataSource object.

The method that is identified by the InsertMethod property can be an instance method or a static (Shared in Visual Basic) method. If it is an instance method, the business object is created and destroyed each time the method specified by the InsertMethod property is called. You can handle the ObjectCreated and ObjectCreating events to work with the business object before the InsertMethod property is called. You can also handle the ObjectDisposing event that is raised after the method that is specified by the DeleteMethod property is called. If the business object implements the IDisposable interface, the Dispose method is called before the object is destroyed. If the method is static (Shared in Visual Basic), the business object is never created and you cannot handle the ObjectCreated, ObjectCreating, and ObjectDisposing events.

Parameters are added to the InsertParameters collection from three sources:

  • From the data-bound control, at run time.

  • From the InsertParameters element, declaratively.

  • From the Inserting method, programmatically.

First, any parameters that are generated from data-bound controls are added to the InsertParameters collection. For example, if the ObjectDataSource control is bound to a GridView control that has the columns Name and Number, the parameters for Name and Number are added to the collection. The data type of these parameters is string. Next, the parameters that are listed in the InsertParameters element are added. If a parameter in the InsertParameters element is found with the same name as a parameter that is already in the InsertParameters collection, the existing parameter is modified to match the parameter that is specified in the InsertParameters element. Typically, this is used to modify the type of the data in the parameter. Finally, you can programmatically add and remove parameters in the Inserting event, which occurs before the Insert method is run. The method is resolved after the parameters are merged. Method resolution is discussed in the next section.

System_CAPS_security Security Note

You should validate any parameter value that you receive from the client. The runtime simply substitutes the parameter value into the InsertMethod property.

When the Insert method is called, the data fields from the data-bound control, the parameters that were created declaratively in the InsertParameters element, and the parameters that were added in the Inserting event handler are all merged. (For more information, see the preceding section.) The ObjectDataSource object then attempts to find a method to call. First, it looks for one or more methods with the name that is specified in the InsertMethod property. If no match is found, an InvalidOperationException exception is thrown. If a match is found, it then looks for matching parameter names. For example, suppose a type that is specified by the TypeName property has two methods named InsertARecord. One InsertARecord has one parameter, ID, and the other InsertARecord has two parameters, Name and Number. If the InsertParameters collection has only one parameter named ID, the InsertARecord method with just the ID parameter is called. The type of the parameter is not checked in resolving the methods. The order of the parameters does not matter.

If the DataObjectTypeName property is set, the method is resolved in a different way. The ObjectDataSource looks for a method with the name that is specified in the InsertMethod property that takes one parameter of the type that is specified in the DataObjectTypeName property. In this case, the name of the parameter does not matter.

This section contains two code examples. The first code example demonstrates how to use an ObjectDataSource object with a business object and a DetailsView control to insert data. The second code example provides an example of the middle-tier business object that is used in the first code example.

The following code example demonstrates how to use an ObjectDataSource control with a business object and a DetailsView control to insert data. Initially, the DetailsView displays a new NorthwindEmployee record, along with an automatically generated Insert button. After you enter data into the fields of the DetailsView control, click the Insert button. The InsertMethod property identifies which method performs the insert operation.

In this example, the UpdateEmployeeInfo method is used to perform an insert; however, it requires a NorthwindEmployee parameter to insert the data. For this reason, the collection of strings that the DetailsView control passes automatically is not sufficient. The NorthwindEmployeeInserting delegate is an ObjectDataSourceMethodEventHandler object that handles the Inserting event and enables you to manipulate the input parameters before the Insert operation proceeds. Because the UpdateEmployeeInfo method requires a NorthwindEmployee object as a parameter, one is created using the collection of strings and is added to the InputParameters collection using a parameter name (ne) that the method expects. You might perform steps like these when using an existing middle-tier object as a data source with types and methods that are not designed specifically for use with the ObjectDataSource control.

When the Insert operation is performed, the method that is identified by the InsertMethod property is called. If the Insert method of the object has a method signature that includes parameters, the InsertParameters collection must contain a parameter with names that match the method signature parameters for the Insert method to complete successfully.

No code example is currently available or this language may not be supported.

The following code example provides an example of a middle-tier business object that the preceding code example uses. The code example consists of two basic classes:

  • The EmployeeLogic class, which is a stateless class that encapsulates business logic.

  • The NorthwindEmployee class, which is a model class that contains only the basic functionality that is required to load and persist data from the data tier.

An additional NorthwindDataException class is provided as a convenience. For a complete working example, you must compile and use these classes. The UpdateEmployeeInfo method is not completely implemented, so you will not insert data into the Northwind Traders database when you experiment with this example.

No code example is currently available or this language may not be supported.

.NET Framework
Available since 2.0
Return to top