Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
SQL Server

How to use ObjectDataSource with GridView in ASP.NET

Author: Richard Wand
Download Source Code :

This article will explain how we can use ObjectDataSource with GridView in ASP.NET.



ObjectDataSource control helps to work with objects and bind these objects to data bound controls like GridView and DetailsView. We can write data access class and write our methods in it for getting, inserting, updating and deleting data. These methods can be invoked using ObjectDataSource to manipulate data in database. When objects are mapped to ObjectDataSource then we can bind this ObjectDataSource to data bound control.

When you write a method for Select command then this method should return a possible collection like DataSet, DataTable, array or list of objects. You can also write methods for delete, insert and update commands.


  1. Create new website and add a web form to it
  2. Add a GridView control and a ObjectdataSource control in web form

    <asp:GridView ID="GridView1" runat="server">
    <asp:ObjectDataSource ID="ObjectDataSource1" runat="server"></asp:ObjectDataSource>
  3. Now add a class to your website and rename it as ProductsClass
  4. Write code below in ProductsClass

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Data;
    using System.Data.SqlClient;
    public class ProductsClasss
        public static DataTable GetProducts()
            string cmdText = "Select * From Products";
            string myConnstring = "Data Source=Your Server;Initial Catalog=NORTHWNDDatabase;Integrated Security=True";
            SqlConnection connection = new SqlConnection(myConnstring);
            SqlCommand command = new SqlCommand(cmdText, connection);
            SqlDataAdapter da = new SqlDataAdapter(command);
            DataTable dt = new DataTable();
            return dt;
        public static void DeleteProduct(int ProductID)
        public static void UpdateProduct(int ProductID)
    Imports Microsoft.VisualBasic
    Imports System.Data
    Imports System.Data.SqlClient
    Public Class ProductsClass
        Public Shared Function GetProducts() As DataTable
            Dim cmdText As String = "Select * From Products"
            Dim myConnstring As String = "Data Source=Your Server;Initial Catalog=NORTHWNDDatabase;Integrated Security=True"
            Dim connection As New SqlConnection(myConnstring)
            Dim command As New SqlCommand(cmdText, connection)
            Dim da As New SqlDataAdapter(command)
            Dim dt As New DataTable()
            Return dt
        End Function
        Public Shared Sub DeleteProduct(ByVal ProductID As Integer)
        End Sub
        Public Shared Sub UpdateProduct(ByVal ProductID As Integer)
        End Sub
    End Class
    GetProducts() function returns DataTable. This function gets data from Products table in NORTHWND database. There are other two methods in class. You can write code for DeleteProduct() and UpdateProduct() functions as I have left these empty. You can also write function to insert data.
  5. Now configure ObjectDataSource from smart arrow at the top
  6. Select ProductsClass in Choose your business object
  7. Then you will see Define Data Methods window
  8. Select GetProducts() method in SELECT tab
  9. You can select DeleteProduct() method in DELETE tab and UpdateProduct() method in UPDATE tab. Click Finish.
  10. Now click on smart arrow at the top of GridView control and choose Data Source ObjectDataSoure1. You have to Enable Editing and Deleting to update and delete.
  11. Code for GridView control and ObjectDataSource will be like this

    <asp:GridView ID="GridView1" runat="server" AllowPaging="True"
        AllowSorting="True" DataSourceID="ObjectDataSource1">
            <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
    <asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
        DeleteMethod="DeleteProduct" SelectMethod="GetProducts"
        TypeName="ProductsClass" UpdateMethod="UpdateProduct">
             <asp:Parameter Name="productID" Type="Int32" />
             <asp:Parameter Name="productID" Type="Int32" />

  12. Now start debugging and see GridView control displaying data.


Add Article Comment:
Name :
Email Address :
Comments :
<< How to perform bulk copy from one database table to another in ASP.NET

Disclaimer - Privacy
© 2002-2017