Display Data Using ObjectDataSource

This article explains how to use ObjectDataSource and how to get data and display it in a GridView and FormView using an ObjectDataSource.

What ObjectDataSource is

The ObjectDataSource serves as a proxy for working with some other object. To configure the ObjectDataSource, we specify the underlying object and how its methods map to the ObjectDataSource's Select, Insert, Update, and Delete methods. Once this underlying object has been specified and its methods mapped to the ObjectDataSource's, we can then bind the ObjectDataSource to a data web control. ASP.NET ships with many data web controls, including the GridView, DetailsView, RadioButtonList, and Dropdown List, among others.

I wonder how I can forget to write about ObJectDataSource when I was writing about data sources. J

ASP.NET 4.0 has seven builtin data sources.

Getting Started

Begin using the following procedure:

  • Start Visual Studio
  • Create a new website
  • Provide the name and location of the website
  • Click "Next"

Now add the connection string in the config file.

  1. <connectionStrings>    
  2.       <add name="ConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\NORTHWND.MDF;Integrated Security=True;User Instance=True"  ="" providerName="System.Data.SqlClient" />    
  3. </connectionStrings> 

Now add a new class and write a function or methods.

EmployeeBLL.cs

  1. using System.Data;    
  2. using System.Data.SqlClient;    
  3. using System.Configuration;    
  4.     SqlConnection con;    
  5.     SqlCommand cmd;    
  6.     SqlDataAdapter da;    
  7.     DataSet ds;    
  8.     /// <summary>    
  9.     /// This function gets employee list    
  10.     /// </summary>    
  11.     /// <returns></returns>    
  12.     public DataSet GetEmployees()    
  13.     {    
  14.         con = new SqlConnection();    
  15.         con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;    
  16.         cmd = new SqlCommand();    
  17.         cmd.CommandText = "SELECT [LastName], [FirstName], [City], [Region], [Country], [PostalCode], [BirthDate], [Photo], [EmployeeID] FROM [Employees]";    
  18.         cmd.Connection = con;    
  19.         da = new SqlDataAdapter(cmd);    
  20.         ds = new DataSet();    
  21.         da.Fill(ds);    
  22.         return ds;    
  23.     } 

Now drag and drop ObjectDataSource, GridView and FormView controls to the page from the toolbox.

img1.jpg 
Image 1.
 
Now choose your business object and click "Next".

img2.jpg
Image 2.
 
Now choose the method and click "Finish".

img3.jpg
Image 3.

img4.jpg

Image 4.

GridView

  1. <h1>Employee List GridView Example</h1>  
  2.     <div>  
  3.         <asp:GridView ID="GridView1" runat="server" AllowPaging="True"  
  4.             AllowSorting="True" CellPadding="4" DataSourceID="ObjectDataSource1"  
  5.             ForeColor="#333333" GridLines="None" AutoGenerateColumns="False"  
  6.             PageSize="5" >  
  7.             <AlternatingRowStyle BackColor="White" />  
  8.             <Columns>  
  9.                 <asp:CommandField ShowSelectButton="True" />  
  10.                 <asp:BoundField DataField="LastName" HeaderText="Last Name" />  
  11.                 <asp:BoundField DataField="FirstName" HeaderText="First Name" />  
  12.                 <asp:BoundField DataField="BirthDate" HeaderText="BirthDate" />  
  13.                 <asp:BoundField DataField="City" HeaderText="City" />  
  14.                 <asp:BoundField DataField="Region" HeaderText="Region" />  
  15.                 <asp:BoundField DataField="PostalCode" HeaderText="PostalCode" />  
  16.                 <asp:BoundField DataField="Country" HeaderText="Country" />                                 
  17.             </Columns>  
  18.             <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />  
  19.             <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />  
  20.             <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />  
  21.             <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />  
  22.             <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />  
  23.             <SortedAscendingCellStyle BackColor="#FDF5AC" />  
  24.             <SortedAscendingHeaderStyle BackColor="#4D0000" />  
  25.             <SortedDescendingCellStyle BackColor="#FCF6C0" />  
  26.             <SortedDescendingHeaderStyle BackColor="#820000" />  
  27.         </asp:GridView>  
  28.         <asp:ObjectDataSource ID="ObjectDataSource1" runat="server"  
  29.             SelectMethod="GetEmployees" TypeName="EmployeeBLL"></asp:ObjectDataSource>  
  30.     </div> 

FormView

  1. <h1>Employee List FormView Example</h1>  
  2. <div>  
  3.  <asp:Panel ID="Panel1" runat="server" ForeColor="Green">   
  4.        <asp:FormView    
  5.            ID="FormView1"    
  6.            runat="server"   
  7.            DataSourceID="ObjectDataSource1"   
  8.            AllowPaging="true">   
  9.            <ItemTemplate>   
  10.                <b>Employee ID: </b>   
  11.                <%# Eval("EmployeeID") %>   
  12.                <br />   
  13.                <b>Name: </b>   
  14.                <%# Eval("LastName") %> <%# Eval("FirstName") %>   
  15.                <br />   
  16.                <b>BirthDate: </b>   
  17.                <%# Eval("BirthDate")%>   
  18.                <br />   
  19.                <b>City: </b>    
  20.                <%# Eval("City") %>   
  21.                <br />   
  22.                 <b>Region: </b>   
  23.                <%# Eval("Region")%>   
  24.                <br />   
  25.                 <b>PostalCode: </b>   
  26.                <%# Eval("PostalCode")%>   
  27.                <br />   
  28.                <b>Country: </b>   
  29.                <%# Eval("Country") %>   
  30.            </ItemTemplate>   
  31.        </asp:FormView>   
  32.    </asp:Panel>   
  33. </div> 

Hit F5 to see the output.

img5.jpg
Image 5.

img6.jpg
Image 6.

For more information, download the attached sample application with the database.


Similar Articles