How to use ObjectDataSource in asp.net

asp.net ObjectDataSource: how to use
ObjectDataSource.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ObjectDataSource.aspx.cs" Inherits="ObjectDataSource" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>asp.net ObjectDataSource: how to use</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Green">ObjectDataSource</h2>
        <asp:ObjectDataSource 
            ID="ObjectDataSource1"
            runat="server"
            >
        </asp:ObjectDataSource>
        <asp:DropDownList 
            ID="DropDownList1"
            runat="server"
            >
        </asp:DropDownList>
    </div>
    </form>
</body>
</html>
ObjectDataSource.aspx.cs
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class ObjectDataSource : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!this.IsPostBack)
        {
            ObjectDataSource1.TypeName = "Category";
            ObjectDataSource1.SelectMethod = "GetCategories";
            DropDownList1.DataSourceID = "ObjectDataSource1";
            DropDownList1.DataTextField = "CategoryName";
            DropDownList1.DataValueField = "CategoryID";
            DropDownList1.BackColor = System.Drawing.Color.Crimson;
            DropDownList1.ForeColor = System.Drawing.Color.Snow;
        }
    }
}
Category.cs
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public class Category
{
 public Category(){}

    public SqlDataReader GetCategories() {
        SqlConnection conn = new SqlConnection();
        conn.ConnectionString = WebConfigurationManager.ConnectionStrings["Northwind"].ConnectionString;
        SqlCommand command = new SqlCommand();
        command.CommandText = "SELECT CategoryID, CategoryName FROM Categories";
        command.Connection = conn;
        SqlDataReader reader;
        conn.Open();
        reader = command.ExecuteReader(CommandBehavior.CloseConnection);
        return reader;

    }
}




Related asp.net example