OneStopTesting.com - Testing EBooks, Tutorials, Articles, Jobs, Training Institutes etc.
OneStopGate.com - Gate EBooks, Tutorials, Articles, FAQs, Jobs, Training Institutes etc.
OneStopMBA.com - MBA EBooks, Tutorials, Articles, FAQs, Jobs, Training Institutes etc.
OneStopIAS.com - IAS EBooks, Tutorials, Articles, FAQs, Jobs, Training Institutes etc.
OneStopSAP.com - SAP EBooks, Tutorials, Articles, FAQs, Jobs, Training Institutes etc.
OneStopGRE.com - of GRE EBooks, Tutorials, Articles, FAQs, Jobs, Training Institutes etc.
Working with ADO.NET 2.0 ObjectDataSource Control | Articles | Recent Articles | News Article | Interesting Articles | Technology Articles | Articles On Education | Articles On Corporate | Company Articles | College Articles | Articles on Recession
Home » Articles » Working with ADO.NET 2.0 ObjectDataSource Control
Working with ADO.NET 2.0 ObjectDataSource Control
Article Posted On Date : Thursday, March 22, 2012
Working with ADO.NET 2.0 ObjectDataSource Control
Advertisements
Introduction As I said in my previous article, the main objective of new controls available in ASP.NET 2.0 is to reduce the code. ObjectDataSource control is one of these new data controls. 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. 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 Data Set Now we need to create a data set. 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. 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. 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. 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. 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. 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.
Amazon.in Widgets