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)

public string TypeName { get; set; }

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.CS" Assembly="Samples.AspNet.CS" %>
<%@ Page language="c#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="http://www.w3.org/1999/xhtml" >
    <title>ObjectDataSource - C# Example</title>
    <form id="Form1" method="post" runat="server">

          datasourceid="ObjectDataSource1" />

          typename="Samples.AspNet.CS.EmployeeLogic" />


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

namespace Samples.AspNet.CS {

using System;
using System.Collections;
using System.Collections.Specialized;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;
  // EmployeeLogic is a stateless business object that encapsulates
  // the operations one can perform on a NorthwindEmployee object.
  public class EmployeeLogic {

    // Returns a collection of NorthwindEmployee objects.
    public static ICollection GetAllEmployees () {
      ArrayList al = new ArrayList();

      ConnectionStringSettings cts = ConfigurationManager.ConnectionStrings["NorthwindConnection"];

      SqlDataSource sds
        = new SqlDataSource(cts.ConnectionString, "SELECT EmployeeID FROM Employees");

      try {

        IEnumerable IDs = sds.Select(DataSourceSelectArguments.Empty);

        // Iterate through the Enumeration and create a
        // NorthwindEmployee object for each ID.
        foreach (DataRowView row in IDs) {
          string id = row["EmployeeID"].ToString();
          NorthwindEmployee nwe = new NorthwindEmployee(id);
          // Add the NorthwindEmployee object to the collection.
      finally {
        // If anything strange happens, clean up.

      return al;
    public static NorthwindEmployee GetEmployee(object anID) {
      return new NorthwindEmployee(anID);

    public static void UpdateEmployeeInfo(NorthwindEmployee ne) {
      bool retval = ne.Save();
      if (! retval) { throw new NorthwindDataException("UpdateEmployee failed."); }

    public static void DeleteEmployee(NorthwindEmployee ne) { }


  public class NorthwindEmployee {

    public NorthwindEmployee () {
      ID = DBNull.Value;
      lastName = "";
      firstName = "";
      titleOfCourtesy = "";
      reportsTo = -1;

    public NorthwindEmployee (object anID) {
      this.ID = anID;

      ConnectionStringSettings cts = ConfigurationManager.ConnectionStrings["NorthwindConnection"];

		SqlConnection conn = new SqlConnection (cts.ConnectionString);
      SqlCommand sc =
        new SqlCommand(" SELECT FirstName,LastName,Title,TitleOfCourtesy,ReportsTo " +
                       " FROM Employees " +
                       " WHERE EmployeeID = @empId",
      // Add the employee ID parameter and set its value.
      sc.Parameters.Add(new SqlParameter("@empId",SqlDbType.Int)).Value = Int32.Parse(anID.ToString());
      SqlDataReader sdr = null;

      try {
        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["FirstName"].ToString();
          this.lastName         = sdr["LastName"].ToString();
          this.title            = sdr["Title"].ToString();
          this.titleOfCourtesy  = sdr["TitleOfCourtesy"].ToString();
          if (! sdr.IsDBNull(4)) {
            this.reportsTo        = sdr.GetInt32(4);
        else {
          throw new NorthwindDataException("Data not loaded for employee id.");
      finally {
        try {
          if (sdr != null) sdr.Close();
        catch (SqlException) {
          // Log an event in the Application Event Log.

    private object ID;

    private string lastName;
    public string LastName {
      get { return lastName; }
      set { lastName = value; }

    private string firstName;
    public string FirstName {
      get { return firstName; }
      set { firstName = value;  }

    private string title;
    public String Title {
      get { return title; }
      set { title = value; }

    private string titleOfCourtesy;
    public string Courtesy {
      get { return titleOfCourtesy; }
      set { titleOfCourtesy = value; }

    private int    reportsTo;
    public int Supervisor {
      get { return reportsTo; }
      set { reportsTo = value; }

    public bool Save () {
      return true;

  internal class NorthwindDataException: Exception {
    public NorthwindDataException(string msg) : base (msg) { }

.NET Framework
Available since 2.0
Return to top