Working With ObjectDataSource And GridView

1. Create Middle Tier class

l Create static, stateless methods

l Mark class and methods with DataObjectMethod attribute (requires using System.ComponentModel)

2. Configure Data Source

· Choose a business object

· Define data methods

3. Configure GridView

· Check “Enable Editing”, etc.

step1.Add a new project in VS.net

step2. Add a class CustomersDataObject in App_Code Folder and add following code

using System;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.IO;
using System.Web;

/// <summary>
/// Summary description for CustomerDataObject
/// </summary>
[DataObject(true)]
public class CustomersDataObject
{
/// <summary>
/// 
/// </summary>
private DataSet _customers;

/// <summary>
/// 
/// </summary>
public CustomersDataObject()
{
  this._customers = HttpContext.Current.Session["Customers"] as DataSet;

  if (this._customers == null)
  {
      this._customers = new DataSet();
      this._customers.ReadXml(HttpContext.Current.Server.MapPath(@"App_Data\customers.xml"));

      HttpContext.Current.Session["Customers"] = this._customers;
  }
}

/// <summary>
/// 
/// </summary>
private DataTable CustomerTable
{
  get { return this._customers.Tables["customers"]; }
}

/// <summary>
/// 
/// </summary>
/// <returns></returns>
[DataObjectMethod(DataObjectMethodType.Select)]
public DataView Select()
{
  this.CustomerTable.DefaultView.Sort = "CustomerID";
  return this.CustomerTable.DefaultView;
}
}
step3 .Place the xml file in App_Data Folder
<?xml version="1.0" encoding="utf-8" ?>
<customers>
<customers>
   <CustomerID>ALFKI</CustomerID>
   <CompanyName>Alfreds Futterkiste</CompanyName>
   <ContactName>Maria Anders</ContactName>
   <ContactTitle>Sales Representative</ContactTitle>
   <Address>Obere Str. 57</Address>
   <City>Berlin</City>
   <PostalCode>12209</PostalCode>
   <Country>Germany</Country>
   <Phone>030-0074321</Phone>
   <Fax>030-0076545</Fax>
</customers>
<customers>
   <CustomerID>ANATR</CustomerID>
   <CompanyName>Ana Trujillo Emparedados y helados</CompanyName>
   <ContactName>Ana Trujillo</ContactName>
   <ContactTitle>Owner</ContactTitle>
   <Address>Avda. de la Constitución 2222</Address>
   <City>México D.F.</City>

   <PostalCode>05021</PostalCode>
   <Country>Mexico</Country>
   <Phone>(5) 555-4729</Phone>
   <Fax>(5) 555-3745</Fax>
</customers>
<customers>
   <CustomerID>ANTON</CustomerID>
   <CompanyName>Antonio Moreno Taquería</CompanyName>
   <ContactName>Antonio Moreno</ContactName>
   <ContactTitle>Owner</ContactTitle>
   <Address>Mataderos  2312</Address>
   <City>México D.F.</City>
   <PostalCode>05023</PostalCode>
   <Country>Mexico</Country>
   <Phone>(5) 555-3932</Phone>
</customers>
<customers>
   <CustomerID>AROUT</CustomerID>
   <CompanyName>Around the Horn</CompanyName>
   <ContactName>Thomas Hardy</ContactName>
   <ContactTitle>Sales Representative</ContactTitle>
   <Address>120 Hanover Sq.</Address>
   <City>London</City>
   <PostalCode>WA1 1DP</PostalCode>
   <Country>UK</Country>
   <Phone>(171) 555-7788</Phone>
   <Fax>(171) 555-6750</Fax>
</customers>
<customers>
   <CustomerID>BERGS</CustomerID>
   <CompanyName>Berglunds snabbköp</CompanyName>
   <ContactName>Christina Berglund</ContactName>
   <ContactTitle>Order Administrator</ContactTitle>
   <Address>Berguvsvägen  8</Address>
   <City>Luleå</City>
   <PostalCode>S-958 22</PostalCode>
   <Country>Sweden</Country>
   <Phone>0921-12 34 65</Phone>
   <Fax>0921-12 34 67</Fax>
</customers>
<customers>
   <CustomerID>BLAUS</CustomerID>
   <CompanyName>Blauer See Delikatessen</CompanyName>
   <ContactName>Hanna Moos</ContactName>
   <ContactTitle>Sales Representative</ContactTitle>
   <Address>Forsterstr. 57</Address>
   <City>Mannheim</City>
   <PostalCode>68306</PostalCode>
   <Country>Germany</Country>
   <Phone>0621-08460</Phone>
   <Fax>0621-08924</Fax>
</customers>
<customers>
   <CustomerID>BLONP</CustomerID>
   <CompanyName>Blondesddsl père et fils</CompanyName>
   <ContactName>Frédérique Citeaux</ContactName>
   <ContactTitle>Marketing Manager</ContactTitle>
   <Address>24, place Kléber</Address>
   <City>Strasbourg</City>
   <PostalCode>67000</PostalCode>
   <Country>France</Country>
   <Phone>88.60.15.31</Phone>
   <Fax>88.60.15.32</Fax>
</customers>
<customers>
   <CustomerID>BOLID</CustomerID>
   <CompanyName>Bólido Comidas preparadas</CompanyName>
   <ContactName>Martín Sommer</ContactName>
   <ContactTitle>Owner</ContactTitle>
   <Address>C/ Araquil, 67</Address>
   <City>Madrid</City>
   <PostalCode>28023</PostalCode>
   <Country>Spain</Country>
   <Phone>(91) 555 22 82</Phone>
   <Fax>(91) 555 91 99</Fax>
</customers>
<customers>
   <CustomerID>BONAP</CustomerID>
   <CompanyName>Bon app'</CompanyName>
   <ContactName>Laurence Lebihan</ContactName>
   <ContactTitle>Owner</ContactTitle>
   <Address>12, rue des Bouchers</Address>
   <City>Marseille</City>
   <PostalCode>13008</PostalCode>
   <Country>France</Country>
   <Phone>91.24.45.40</Phone>
   <Fax>91.24.45.41</Fax>
</customers>
<customers>
   <CustomerID>BOTTM</CustomerID>
   <CompanyName>Bottom-Dollar Markets</CompanyName>
   <ContactName>Elizabeth Lincoln</ContactName>
   <ContactTitle>Accounting Manager</ContactTitle>
   <Address>23 Tsawassen Blvd.</Address>
   <City>Tsawassen</City>
   <Region>BC</Region>
   <PostalCode>T2F 8M4</PostalCode>
   <Country>Canada</Country>
   <Phone>(604) 555-4729</Phone>
   <Fax>(604) 555-3745</Fax>
</customers>
<customers>
   <CustomerID>BSBEV</CustomerID>
   <CompanyName>B's Beverages</CompanyName>
   <ContactName>Victoria Ashworth</ContactName>
   <ContactTitle>Sales Representative</ContactTitle>
   <Address>Fauntleroy Circus</Address>
   <City>London</City>
   <PostalCode>EC2 5NT</PostalCode>
   <Country>UK</Country>
   <Phone>(171) 555-1212</Phone>
</customers>
<customers>
   <CustomerID>CACTU</CustomerID>
   <CompanyName>Cactus Comidas para llevar</CompanyName>
   <ContactName>Patricio Simpson</ContactName>
   <ContactTitle>Sales Agent</ContactTitle>
   <Address>Cerrito 333</Address>
   <City>Buenos Aires</City>
   <PostalCode>1010</PostalCode>
   <Country>Argentina</Country>
   <Phone>(1) 135-5555</Phone>
   <Fax>(1) 135-4892</Fax>
</customers>
<customers>
   <CustomerID>CENTC</CustomerID>
   <CompanyName>Centro comercial Moctezuma</CompanyName>
   <ContactName>Francisco Chang</ContactName>
   <ContactTitle>Marketing Manager</ContactTitle>
   <Address>Sierras de Granada 9993</Address>
   <City>México D.F.</City>
   <PostalCode>05022</PostalCode>
   <Country>Mexico</Country>
   <Phone>(5) 555-3392</Phone>
   <Fax>(5) 555-7293</Fax>
</customers>
<customers>
   <CustomerID>CHOPS</CustomerID>
   <CompanyName>Chop-suey Chinese</CompanyName>
   <ContactName>Yang Wang</ContactName>
   <ContactTitle>Owner</ContactTitle>
   <Address>Hauptstr. 29</Address>
   <City>Bern</City>
   <PostalCode>3012</PostalCode>
   <Country>Switzerland</Country>
   <Phone>0452-076545</Phone>
</customers>
<customers>
   <CustomerID>COMMI</CustomerID>
   <CompanyName>Comércio Mineiro</CompanyName>
   <ContactName>Pedro Afonso</ContactName>
   <ContactTitle>Sales Associate</ContactTitle>
   <Address>Av. dos Lusíadas, 23</Address>
   <City>Sao Paulo</City>
   <Region>SP</Region>
   <PostalCode>05432-043</PostalCode>
   <Country>Brazil</Country>
   <Phone>(11) 555-7647</Phone>
</customers>
<customers>
   <CustomerID>CONSH</CustomerID>
   <CompanyName>Consolidated Holdings</CompanyName>
   <ContactName>Elizabeth Brown</ContactName>
   <ContactTitle>Sales Representative</ContactTitle>
   <Address>Berkeley Gardens 12  Brewery</Address>
   <City>London</City>
   <PostalCode>WX1 6LT</PostalCode>
   <Country>UK</Country>
   <Phone>(171) 555-2282</Phone>
   <Fax>(171) 555-9199</Fax>
</customers>
</customers>
step4. Last and final step drag a gridview on web page and assign DataSourceID="odsCustomers"
<%@ 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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
    <div>
        <asp:ObjectDataSource ID="odsCustomers" runat="server" SelectMethod="Select" TypeName="CustomersDataObject" />
        <asp:GridView ID="GridView1" runat="Server" DataSourceID="odsCustomers">
        </asp:GridView>
    </div>
</form>
</body>
</html>

No comments:

Post a Comment