Blue Theme Orange Theme Green Theme Red Theme
 
Nevron Chart
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
DevExpress UI Controls
Search :       Advanced Search »
Home » ADO.NET & Database » Object DataSource in ASP.NET 2.0

Object DataSource in ASP.NET 2.0

ObjectDataSource control is one of these new data control added to ADO.NET 2.0. This control is used to bind objects to data-bound controls. In this step by step tutorial, I will discuss how to use ObjectDataSource control in your data-driven Web applications.

Author Rank :
Page Views : 35985
Downloads : 0
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
Discover the top 5 tips for understanding .NET Interop
Become a Sponsor
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


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.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Mahesh Chand
Mahesh is the founder of C# Corner and Mindcracker Network, an author of several .NET programming books and a Microsoft MVP for 6 consecutive years. In his day to day work, Mahesh is a Senior Software Consultant with over 14 years of IT industry experience building systems for Financial and Banking, Engineering & Architectural, Imaging, Construction, Biological & Pharmaceuticals, Healthcare and Education industries. His expertise is Windows Forms, ASP.NET, Silverlight, WPF, WCF, Visual Studio 2010, SQL Server, and Oracle.  If you are looking for a Sharepoint, Windows Forms, ASP.NET, WPF, Silverlight, C#, VB.NET, Oracle, and SQL Server Consultant in Philadelphia area or remote location, drop me a line at MAHESH [AT] C-SHARPCORNER [DOT] COM.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the Top 5 .NET Memory Management Fundamentals
To write the best .NET code, you need to know exactly how the .NET framework really manages memory. Ricky Leeks presents the Top 5 fundamental facts of .NET memory management. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Team Foundation Server Hosting
Become a Sponsor
 Comments
Good Article by Pal On August 14, 2008
This is really good article to read
Reply | Email | Modify 
Good Article by Pal On August 14, 2008
This is really good article to read
Reply | Email | Modify 
Re: Good Article by Sowmya On August 13, 2009
Very informative,the only article thats better than the Microsoft training kit!

Reply | Email | Modify 
DevExpress Free UI Controls
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.