Object DataSource in ASP.NET 2.0


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;
list.Add(row);
row =
new AuthorData();
row.Name = "Jack Black";
row.Age = 2;
row.Consultant =
false;
list.Add(row);
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.



Figure 1. Configure Data Source link

Which 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 gets listed here automatically. I select BusinessHelper class from the list. See Figure 2.

Figure 2. 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.

Figure 3. 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.

Figure 4. 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.

Figure 5. The Output

Summary

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


Similar Articles
Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.