Support us .Net Basics C# SQL ASP.NET ADO.NET MVC Slides C# Programs Subscribe Buy DVD

Object datasource in - Part 3

Suggested Videos 
Part 1 - Datagrid in
Part 2 - Data source controls in

In Part 2 of the gridview tutorial, we discussed about sqldatasource controls. Please watch Part 2, before proceeding with this video.

Sqldatasource control include sql queries, in the HTML of the aspx page. This may lead to the complexity of the project. That's why, in real time, sqldatasource control is very rarely used. 

Let us understand object datasource, with an example. 

Let's assume our project has the following 3 layers.
1. Presentation Layer - This includes the web application project, where we have webforms, their code-behind files etc.
2. Business Logic Layer - This is usually a class library project, which includes business objects, like customers, orders, products etc.
3. Data Access Layer - This is usually a class library project, that is responsible for all the database CRUD(Create, Read, Update and Delete) operations.

The above 3 layers interact in the following way.
Presentation Layer => Business Logic Layer => Data Access Layer => Database

Presentation Layer, calls Business Logic Layer which in turn will call, Data Access Layer. Data Access Layer performs all the CRUD operations on the database.

In reality, we may have each layer in it's own project, but for the purpose of this demo, we will include the data access layer classes in our web application. We will discuss about building an web application with n-tier architecture and the difference between layers and tiers in a later video session.

Create an web application, and add a class file with name,  ProductDataAccessLayer.cs. Copy and paste the following code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace Demo
    public class Product
        public int Id { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }

    public class ProductDataAccessLayer
        public static List<Product> GetAllProducts()
            List<Product> listProducts = new List<Product>();

            string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
            using (SqlConnection con = new SqlConnection(CS))
                SqlCommand cmd = new SqlCommand("select * from tblProducts", con);
                SqlDataReader rdr = cmd.ExecuteReader();
                while (rdr.Read())
                    Product product = new Product();
                    product.Id = Convert.ToInt32(rdr["Id"]);
                    product.Name = rdr["Name"].ToString();
                    product.Description = rdr["Description"].ToString();


            return listProducts;

At this point, drag "ObjectDataSource" control from the toolbox and drop it on WebForm1.aspx. Steps to configure "ObjectDataSource" 

1. Flip the webform to design mode
2. Click on the little right arrow(Smart Tag). This should display "ObjectDataSource Tasks"
3. Click on "Configure DataSource" link
4. From "Choose your business object" dropdownlist select "Demo.ProductDataAccessLayer" and click "Next"
5. From "Choose a method" dropdownlist, select "GetAllProducts()" and click "Finish"

At this point, we are done, configuring ObjectDataSource control. Now, drag a GridView control from the toolbox and drop it on the webform. Finally associate "ObjectDataSource1" with "GridView1" control using DataSourceID property of "GridView1" control. Run the application and data from "tblProducts" table should now be displayed in the gridview control.

The HTML on WebForm1.aspx should be as shown below
    <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
        SelectMethod="GetAllProducts" TypeName="Demo.ProductDataAccessLayer">
    <br />
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            <asp:BoundField DataField="Id" HeaderText="Id" SortExpression="Id" />
            <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
            <asp:BoundField DataField="Description" HeaderText="Description" 
                SortExpression="Description" />


  1. ThnaQ very much venkat for posting wonderful videos

  2. Sir if possible please post tutorial for 3- tier architecture.

  3. Thank you sir, i learnt lot of things from your tutorial.awesome sir you are great.
    please provide any post on 3-tier architecture or model

  4. Hi,
    Can you please add an tree - tier architecture example for object datasource...


If you like this website, please share with your friends on facebook and Google+ and recommend us on google using the g+1 button on the top right hand corner.