Export (0) Print
Expand All

ObjectDataSource.TypeName Property

Gets or sets the name of the class that the ObjectDataSource object represents.

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

'Declaration
Public Property TypeName As String
<asp:ObjectDataSource TypeName="String" />

Property Value

Type: System.String
A partially or fully qualified class name that identifies the type of the object that the ObjectDataSource represents. The default is an empty string ("").

To create an instance of the object that the ObjectDataSource control binds to, the control uses reflection to load the type that is identified by the type name at run time. Therefore, the value of the TypeName property can be a partially qualified type for code that is located in the Bin or App_Code directories or a fully qualified type name for code that is registered in the global assembly cache. If you use the global assembly cache, you must add the appropriate reference to the assemblies section of the Machine.config or Web.config file.

The type must have a default constructor, unless you handle the ObjectCreating event to create an instance of it yourself. An instance of the type is created for each call to the Select, Update, Insert, and Delete methods, if the methods on the type are member methods. An instance is not created if the methods are static (Shared in Visual Basic). If the type implements the IDisposable interface, the Dispose method is called before the object is destroyed.

The following code example demonstrates how a GridView control can display data by using an ObjectDataSource control on a Web Forms page. The ObjectDataSource identifies a partially or fully qualified class name with its TypeName property and a method that is called to retrieve data with its SelectMethod property. At run time, the object is created and the method is called using reflection. The GridView control enumerates through the IEnumerable collection that is returned by the SelectMethod property, and displays the data.

<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.VB" Assembly="Samples.AspNet.VB" %>
<%@ Page language="vb" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head>
    <title>ObjectDataSource - Visual Basic Example</title>
  </head>
  <body>
    <form id="Form1" method="post" runat="server">

        <asp:gridview
          id="GridView1"
          runat="server"
          datasourceid="ObjectDataSource1" />

        <asp:objectdatasource
          id="ObjectDataSource1"
          runat="server"
          selectmethod="GetAllEmployees"
          typename="Samples.AspNet.VB.EmployeeLogic" />

    </form>
  </body>
</html>

The following example shows the class that the TypeName property value refers to.

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. 
' When the class is written in Visual Basic, you cannot use the Shared 
' part. 
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")
      Try 
         Dim IDs As IEnumerable = sds.Select(DataSourceSelectArguments.Empty)

         ' Iterate through the Enumeration and create a 
         ' NorthwindEmployee object for each ID. 
         For Each row As DataRowView In IDs
            Dim id As String = row("EmployeeID").ToString()
            Dim nwe As New NorthwindEmployee(id)
            ' Add the NorthwindEmployee object to the collection.
            al.Add(nwe)
         Next 
      Finally 
         ' If anything strange happens, clean up.
         sds.Dispose()
      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 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)
   End Sub 'DeleteEmployee

End Class 'EmployeeLogic


Public Class NorthwindEmployee


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

      Try
         conn.Open()
         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()
            Me.aTitle = sdr("Title").ToString()
            Me.titleOfCourtesy = sdr("TitleOfCourtesy").ToString()
            If Not sdr.IsDBNull(4) Then 
               Me.reportsTo = sdr.GetInt32(4)
            End If 
         Else 
            Throw New NorthwindDataException("Data not loaded for employee id.")
         End If 
      Finally 
         Try 
            If Not (sdr Is Nothing) Then
               sdr.Close()
            End If
            conn.Close()
         Catch se As SqlException
            ' Log an event in the Application Event Log. 
            Throw 
         End Try 
      End Try 
   End Sub 'New 

   Private ID As Object 

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

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

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

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

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

   Public Function Save() As Boolean 
      Return True 
   End Function 'Save
End Class 'NorthwindEmployee


Friend Class NorthwindDataException
   Inherits Exception

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

.NET Framework

Supported in: 4.5.2, 4.5.1, 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.

Show:
© 2014 Microsoft