This documentation is archived and is not being maintained.

ObjectDataSourceStatusEventArgs Class

Provides data for the Selected, Inserted, Updated, and Deleted events of the ObjectDataSource control.

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

<AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level := AspNetHostingPermissionLevel.Minimal)> _
<AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level := AspNetHostingPermissionLevel.Minimal)> _
Public Class ObjectDataSourceStatusEventArgs _
	Inherits EventArgs
Dim instance As ObjectDataSourceStatusEventArgs

The ObjectDataSourceStatusEventArgs class is used in the OnSelected, OnUpdated, OnInserted, and OnDeleted methods to provide access to output parameters, return values, and exceptions that are thrown by the method that is called by the ObjectDataSource control. These are accessed by the OutputParameters, ReturnValue, and Exception properties, respectively. By adding a delegate to handle the Selected, Updated, Inserted, or Deleted events, you can examine this data and perform any additional post processing that is required.

The ObjectDataSource control exposes many events that you can handle to work with the underlying business object at various times in its lifecycle. The following table lists the events and the associated EventArgs classes and event handler delegates.





Occurs immediately before the instance of the business object is created.




Occurs immediately after the instance of the business object is created.




Occurs before the data is retrieved.



Inserting, Updating, and Deleting.

Occur before an insert, update, or delete operation is performed.




Occurs after the data is retrieved.



Inserted, Updated, and Deleted.

Occur after the insert, update, or delete operation is completed.




Occurs before a business object is destroyed.



This section contains two code examples. The first code example demonstrates how to use an ObjectDataSource control with a business object and a GridView control to delete data. The second code example provides an example middle-tier business object that the first code example uses.

The following code example demonstrates how to use an ObjectDataSource control with a business object and a GridView control to delete data. The GridView initially 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 Delete action is performed using the method that is specified by the DeleteMethod property and any parameters that are specified in the DeleteParameters collection. In this code example, one parameter is specified in the DeleteParameters collection that corresponds to the employee ID. This is because although the ID is included in the Columns collection as a BoundField object, it will be passed as a string to the ObjectDataSource control. By adding it explicitly to the DeleteParameters collection with a Type property set to the Int32 value, it will pass correctly by the ObjectDataSource to the method as an integer, rather than as string.

In this code example, some preprocessing and post-processing steps are also performed. The NorthwindEmployeeDeleting delegate is called to handle the Deleting event before the Delete action is performed, and the NorthwindEmployeeDeleted delegate is called to handle the Deleted event after the Delete action has completed, to handle any exceptions that might have occurred. The NorthwindEmployeeDeleted delegate accesses possible exceptions that are thrown by the business object that is using the ObjectDataSourceStatusEventArgs object. In this example, if a NorthwindDataException is thrown, it is handled by this delegate.

<%@ 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">
' Called before a Delete operation. 
    Private Sub NorthwindEmployeeDeleting(ByVal source As Object, ByVal e As ObjectDataSourceMethodEventArgs)

        ' The GridView passes the ID of the employee 
        ' to be deleted. However, the business object, EmployeeLogic, 
        ' requires a NorthwindEmployee parameter, named "ne". Create 
        ' it now and add it to the parameters collection. 
        Dim paramsFromPage As IDictionary = e.InputParameters

        If Not paramsFromPage("EmpID") Is Nothing Then 

            Dim ne As New NorthwindEmployee(paramsFromPage("EmpID").ToString())
            ' Remove the old EmpID parameter.
            paramsFromPage.Add("ne", ne)

        End If 
    End Sub ' NorthwindEmployeeDeleting

    ' Called after a Delete operation. 
    Private Sub NorthwindEmployeeDeleted(ByVal source As Object, ByVal e As ObjectDataSourceStatusEventArgs)
        ' Handle the Exception if it is a NorthwindDataException. 
        If Not e.Exception Is Nothing Then 

            ' Handle the specific exception type. The ObjectDataSource wraps 
            ' any Exceptions in a TargetInvokationException wrapper, so 
            ' check the InnerException property for the expected Exception types. 
            If e.Exception.InnerException.GetType().Equals(GetType(NorthwindDataException)) Then

                Label1.Text = e.Exception.InnerException.Message
                ' Because the exception is handled, there is 
                ' no reason to throw it.
                e.ExceptionHandled = True 

            End If 
        End If 
    End Sub ' NorthwindEmployeeDeleted
<html xmlns="" >
    <title>ObjectDataSource - VB Example</title>
    <form id="Form1" method="post" runat="server">

            <asp:boundfield headertext="EmpID" datafield="EmpID" />
            <asp:boundfield headertext="First Name" datafield="FirstName" />
            <asp:boundfield headertext="Last Name" datafield="LastName" />

            <asp:parameter name="EmpID" type="Int32" />

        <asp:label id="Label1" runat="server" />


The following code example provides an example middle-tier business object that the preceding code example uses. The following list describes the two major classes that are defined in the code example:

  • The EmployeeLogic class is a class that does not maintain state and encapsulates business logic.

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

In addition, a NorthwindDataException class is provided as a convenience.

This set of example classes uses the Northwind Traders database, which is an example database that is available with Microsoft SQL Server and Microsoft Access. For a complete working example, you must compile this code as a library, and then use these classes from an ASP page. The ASP page that is provided in the preceding example is configured to work with this class library. The UpdateEmployeeInfo method is not completely implemented, so you will not insert data into the Northwind Traders database when you experiment with this 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

   ' Return a collection of NorthwindEmployee objects. 
   Public Shared Function GetAllEmployees() As ICollection
      Dim al As New ArrayList()

      ' Use the SqlDataSource class to wrap the 
      ' ADO.NET code required to query the database. 
      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
      Return New NorthwindEmployee(anID)
   End Function 'GetEmployee

   Public Shared Sub DeleteEmployee(ne As NorthwindEmployee)
      Dim retval As Boolean = ne.Delete()
      If Not retval Then 
         Throw New NorthwindDataException("Employee delete failed.")
      End If ' Delete the object in memory.
      ne = Nothing 
   End Sub 'DeleteEmployee

   Public Shared Sub DeleteEmployeeByID(anID As Integer)
      Dim tempEmp As New NorthwindEmployee(anID)
   End Sub 'DeleteEmployeeByID

End Class 'EmployeeLogic

Public Class NorthwindEmployee

   Public Sub New()
      ID = DBNull.Value
      aLastName = ""
      aFirstName = "" 
   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 " & _
                               " 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()

         ' This is not a while loop. It only loops 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()
            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 Object
         Return ID
      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 Function Delete() As Boolean 
      If ID.Equals(DBNull.Value) Then 
         ' The Employee object is not persisted. 
         Return True 
         ' 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. 
         Dim cts As ConnectionStringSettings = ConfigurationManager.ConnectionStrings("NorthwindConnection")
         Dim sds As New SqlDataSource()
            sds.ConnectionString = cts.ConnectionString
            sds.DeleteParameters.Add(New Parameter("empID", TypeCode.Int32, Me.ID.ToString()))
            sds.DeleteCommand = "DELETE FROM [Order Details] " & _
                "WHERE OrderID IN (SELECT OrderID FROM Orders WHERE EmployeeID=@empID)"
            sds.DeleteCommand = "DELETE FROM Orders WHERE EmployeeID=@empID"
            sds.DeleteCommand = "DELETE FROM EmployeeTerritories WHERE EmployeeID=@empID"
            sds.DeleteCommand = "DELETE FROM Employees WHERE EmployeeID=@empID"
            Return True 
            ' Clean up resources.
         End Try 
      End If 
   End Function 'Delete
End Class 'NorthwindEmployee

Public Class NorthwindDataException
   Inherits Exception

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

End Class 'NorthwindDataException
End Namespace


Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

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

.NET Framework

Supported in: 3.5, 3.0, 2.0