……………………………………………….Expertise in .NET Technologies

Object DataSource in ASP.NET 2.0

Posted by Ravi Varma Thumati on April 29, 2009

Introduction The main objective of new controls available in ASP.NET 2.0 is to reduce the code. ObjectDataSource control is one of the new data control available in ASP.NET 2.0. This control is used to bind objects (classes) to data-bound controls.


In this step by step tutorial, I will discuss how to use ObjectDataSource control in your data-driven Web applications. First, I will create a class and then create a collection of objects of that class type and then bind that collection to a GridView control.

Creating a Class

First I create a class called AuthorData. This class has three public properties – Name, Age, and Consultant. When you bind an object to a data-bound controls, these properties are treated as columns of the control.


using System;
/// <summary>
Summary description for Content
/// </summary>
public class AuthorData
     // Private variables
private string name = null;
     private int age = 0;
     private bool consultant = false;

     public AuthorData()
     public string Name
          get { return name; }
          set { name = value; }
     public int Age
          get { return age; }
          set { age = value; }
    public bool Consultant
         get { return consultant; }
         set { consultant = value; }


Creating a DataSet

Now we need to create a DataSet. We will create a collection of AuthorData objects, which will be used as data for the data bound control. I add a BusinessHelper class, which has a method – GetData. The GetData method returns an ICollection, which is collection of objects. As you can see from the following code, I add two objects to the collection.


using System;
/// <summary>
Summary description for Content
/// </summary>
public class AuthorDatausing System;
     using System.Collections;
     public class BusinessHelper
   public BusinessHelper()
   public ICollection GetData()
       ArrayList list = new ArrayList();
       AuthorData row = new AuthorData();
       row.Name = “Mahesh Chand”;
       row.Age = 29;
       row.Consultant = true;
       row = new AuthorData();
       row.Name = “Jack Black”;
       row.Age = 2;
       row.Consultant = false;
       return list;

Creating an ObjectDataSource

Next we need to create an ObjectDataSource. I simply drag ObjectDataSource control from Toolbox to the page and configure data source using Configure Data Source link. See Figure 1.



Figure1. Configure Data Source link


This launches Configure Data Source dialog! Here you can select an object from the drop down list. All of your classes stored in the Bin or Code folders get listed here automatically. I select BusinessHelper class from the list. See Figure 2.



Figure2. Choosing a business object


You can also select the methods that can be used instead of SELECT, UPDATE, INSERT, and DELETE queries. As you may have noticed we had a method GetData in our BusinessHelper class. This method is available on next page of Configure Data Source dialog. See Figure 3.



Figure3. Select business object methods


Now click Finish button.


Adding a Data Bound Control


Now we add GridView control to the page and set its data source property to ObjectDataSource1. See Figure 4.




Figure4. Setting GridView’s DataSource property


Now we are all set. We run the application and output looks like Figure 5. As you can see from this output, GridView control generates different types of columns corresponding to the data type of the properties of AuthorData class.





Figure5. The Output




ASP.NET version 2.0 provides new data source and data bound controls. In this article, we discussed ObjectDataSource and how to use it to display objects in a GridView control.    


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: