Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

Object datasource in asp.net - Part 3

Suggested Videos 
Part 1 - Datagrid in asp.net
Part 2 - Data source controls in asp.net

In Part 2 of the asp.net 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 asp.net 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 asp.net web application. We will discuss about building an asp.net web application with n-tier architecture and the difference between layers and tiers in a later video session.

Create an asp.net 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);
                con.Open();
                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();

                    listProducts.Add(product);
                }
            }

            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
<div>
    <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
        SelectMethod="GetAllProducts" TypeName="Demo.ProductDataAccessLayer">
    </asp:ObjectDataSource>
    <br />
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataSourceID="ObjectDataSource1">
        <Columns>
            <asp:BoundField DataField="Id" HeaderText="Id" SortExpression="Id" />
            <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
            <asp:BoundField DataField="Description" HeaderText="Description" 
                SortExpression="Description" />
        </Columns>
    </asp:GridView>
</div>

4 comments:

  1. ThnaQ very much venkat for posting wonderful videos

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

    ReplyDelete
  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

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

    ReplyDelete

It would be great if you can help share these free resources