Export (0) Print
Expand All

ObjectDataSourceView.InsertMethod Property

Gets or sets the name of the method or function that the ObjectDataSourceView object invokes to insert data.

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

Public Property InsertMethod As String

Property Value

Type: System.String
A string that represents the name of the method or function that the ObjectDataSourceView uses to insert data. The default value is an empty string ("").

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 event to work with the business object before the method specified by the InsertMethod property is called. You can also handle the ObjectDisposing event that is raised after the method specified by the InsertMethod property is called. (Dispose is called, only if the business object implements the IDisposable interface.) If the method is a static (Shared in Visual Basic) method, the business object is never created and you cannot handle these events.

If the business object that the ObjectDataSource object implements more than one method or function with the same name (method overloads), the data source control attempts to invoke the correct one according to a set of conditions, including the parameters in the InsertParameters collection. If the parameters in the InsertParameters collection do not match those of the method specified by the InsertMethod property signature, the data source throws an exception.

The value of the InsertMethod property is stored in view state.

For more information, see InsertMethod.

This section contains two code examples. The first code example demonstrates how to display filtered data using an ObjectDataSource control 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. The DetailsView initially 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 code 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.

<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.VB" Assembly="Samples.AspNet.VB" %>
<%@ Import namespace="Samples.AspNet.VB" %>
<%@ Page language="vb" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<script runat="server">
Private Sub NorthwindEmployeeInserting(source As Object, e As ObjectDataSourceMethodEventArgs)

  ' The GridView control passes an array of strings in the parameters
  ' collection because that is the type it knows how to work with.
  ' However, the business object expects a custom type. Build it
  ' and add it to the parameters collection.
  Dim paramsFromPage As IDictionary = e.InputParameters

  Dim ne As New NorthwindEmployee()
  ne.FirstName  = paramsFromPage("FirstName").ToString()
  ne.LastName   = paramsFromPage("LastName").ToString()
  ne.Title      = paramsFromPage("Title").ToString()
  ne.Courtesy   = paramsFromPage("Courtesy").ToString()
  ne.Supervisor = Int32.Parse(paramsFromPage("Supervisor").ToString())

  paramsFromPage.Add("ne", ne)
End Sub ' NorthwindEmployeeInserting

<html xmlns="http://www.w3.org/1999/xhtml" >
    <title>ObjectDataSource - VB Example</title>
    <form id="Form1" method="post" runat="server">


          typename="Samples.AspNet.VB.EmployeeLogic" >
            <asp:parameter name="anID" defaultvalue="-1" />


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

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

  • The NorthwindEmployee class is a model class, which 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 code example.

Imports System
Imports System.Collections
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.UI
Imports System.Web.UI.WebControls

Namespace Samples.AspNet.VB

' 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 Shared Function GetAllEmployees() As ICollection

      Dim al As New ArrayList()

      Dim cts As ConnectionStringSettings = ConfigurationManager.ConnectionStrings("NorthwindConnection")

      Dim sds As New SqlDataSource(cts.ConnectionString, "SELECT EmployeeID FROM Employees")

         Dim IDs As IEnumerable = sds.Select(DataSourceSelectArguments.Empty)

         ' Iterate through the Enumeration and create a 
         ' NorthwindEmployee object for each ID. 
         Dim enumerator As IEnumerator = IDs.GetEnumerator()
         While enumerator.MoveNext()
            ' The IEnumerable contains DataRowView objects. 
            Dim row As DataRowView = CType(enumerator.Current,DataRowView)
            Dim id As String = row("EmployeeID").ToString()
            Dim nwe As New NorthwindEmployee(id)
            ' Add the NorthwindEmployee object to the collection.
         End While 
         ' If anything strange happens, clean up.
      End Try 

      Return al
   End Function 'GetAllEmployees

   Public Shared Function GetEmployee(anID As Object) As NorthwindEmployee
      If anID.Equals("-1") OrElse anID.Equals(DBNull.Value) Then 
         Return New NorthwindEmployee()
         Return New NorthwindEmployee(anID)
      End If 
   End Function 'GetEmployee

   Public Shared Sub UpdateEmployeeInfo(ne As NorthwindEmployee)
      Dim retval As Boolean = ne.Save()
      If Not retval Then 
         Throw New NorthwindDataException("UpdateEmployee failed.")
      End If 
   End Sub 'UpdateEmployeeInfo

   Public Shared Sub DeleteEmployee(ne As NorthwindEmployee)
      Dim retval As Boolean = ne.Delete()
      If Not retval Then 
         Throw New NorthwindDataException("DeleteEmployee failed.")
      End If 
   End Sub 'DeleteEmployee

   ' And so on... 

End Class 'EmployeeLogic

Public Class NorthwindEmployee

   Public Sub New()
      ID = DBNull.Value
      lastName = ""
      firstName = ""
      title = ""
      titleOfCourtesy = ""
      reportsTo = - 1
   End Sub 'New 

   Public Sub New(anID As Object)
      Me.ID = anID

      Dim cts As ConnectionStringSettings = ConfigurationManager.ConnectionStrings("NorthwindConnection")

      Dim conn As New SqlConnection(cts.ConnectionString)

      Dim sc As New SqlCommand(" SELECT FirstName,LastName,Title,TitleOfCourtesy,ReportsTo " & _
                               " FROM Employees " & _
                               " WHERE EmployeeID = @empId", conn)

      ' Add the employee ID parameter and set its value.
      sc.Parameters.Add(New SqlParameter("@empId", SqlDbType.Int)).Value = Int32.Parse(anID.ToString())
      Dim sdr As SqlDataReader = Nothing 

         sdr = sc.ExecuteReader()

         ' Only loop once. 
         If Not (sdr Is Nothing) AndAlso sdr.Read() Then 
            ' The IEnumerable contains DataRowView objects. 
            Me.aFirstName = sdr("FirstName").ToString()
            Me.aLastName = sdr("LastName").ToString()
            Me.aTitle = sdr("Title").ToString()
            Me.titleOfCourtesy = sdr("TitleOfCourtesy").ToString()
            If Not sdr.IsDBNull(4) Then 
               Me.reportsTo = sdr.GetInt32(4)
            End If 
            Throw New NorthwindDataException("Data not loaded for employee id.")
         End If 
            If Not (sdr Is Nothing) Then
            End If
         Catch se As SqlException
            ' Log an event in the Application Event Log. 
         End Try 
      End Try 
   End Sub 'New 

   Private ID As Object 
   Public ReadOnly Property EmpID() As String 
         Return ID.ToString()
      End Get 
   End Property 

   Private aLastName As String 
   Public Property LastName() As String 
         Return aLastName
      End Get 
         aLastName = value
      End Set 
   End Property 

   Private aFirstName As String 
   Public Property FirstName() As String 
         Return aFirstName
      End Get 
         aFirstName = value
      End Set 
   End Property 

   Public ReadOnly Property FullName() As String 
         Return FirstName & " " & LastName
      End Get 
   End Property 

   Private aTitle As String 
   Public Property Title() As String 
         Return aTitle
      End Get 
         aTitle = value
      End Set 
   End Property 

   Private titleOfCourtesy As String 
   Public Property Courtesy() As String 
         Return titleOfCourtesy
      End Get 
         titleOfCourtesy = value
      End Set 
   End Property 

   Private reportsTo As Integer 
   Public Property Supervisor() As Integer 
         Return reportsTo
      End Get 
         reportsTo = value
      End Set 
   End Property 

   Public Function Save() As Boolean 

      ' Implement persistence logic. 
      Return True 

   End Function 'Save

   Public Function Delete() As Boolean 

     ' Implement delete logic. 
     Return True 

   End Function 'Delete
End Class 'NorthwindEmployee

Friend Class NorthwindDataException
   Inherits Exception

   Public Sub New(msg As String)
   End Sub 'New 
End Class 'NorthwindDataException

End Namespace

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

© 2014 Microsoft