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.
- <connectionStrings>
- <add name="ConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\NORTHWND.MDF;Integrated Security=True;User Instance=True" ="" providerName="System.Data.SqlClient" />
- </connectionStrings>
Now add a new class and write a function or methods.
EmployeeBLL.cs
- using System.Data;
- using System.Data.SqlClient;
- using System.Configuration;
- SqlConnection con;
- SqlCommand cmd;
- SqlDataAdapter da;
- DataSet ds;
-
-
-
-
- public DataSet GetEmployees()
- {
- con = new SqlConnection();
- con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
- cmd = new SqlCommand();
- cmd.CommandText = "SELECT [LastName], [FirstName], [City], [Region], [Country], [PostalCode], [BirthDate], [Photo], [EmployeeID] FROM [Employees]";
- cmd.Connection = con;
- da = new SqlDataAdapter(cmd);
- ds = new DataSet();
- da.Fill(ds);
- return ds;
- }
Now drag and drop ObjectDataSource, GridView and FormView controls to the page from the toolbox.
Image 1.
Now choose your business object and click "Next".
Image 2.
Now choose the method and click "Finish".
Image 3.
Image 4.
GridView
- <h1>Employee List GridView Example</h1>
- <div>
- <asp:GridView ID="GridView1" runat="server" AllowPaging="True"
- AllowSorting="True" CellPadding="4" DataSourceID="ObjectDataSource1"
- ForeColor="#333333" GridLines="None" AutoGenerateColumns="False"
- PageSize="5" >
- <AlternatingRowStyle BackColor="White" />
- <Columns>
- <asp:CommandField ShowSelectButton="True" />
- <asp:BoundField DataField="LastName" HeaderText="Last Name" />
- <asp:BoundField DataField="FirstName" HeaderText="First Name" />
- <asp:BoundField DataField="BirthDate" HeaderText="BirthDate" />
- <asp:BoundField DataField="City" HeaderText="City" />
- <asp:BoundField DataField="Region" HeaderText="Region" />
- <asp:BoundField DataField="PostalCode" HeaderText="PostalCode" />
- <asp:BoundField DataField="Country" HeaderText="Country" />
- </Columns>
- <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
- <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
- <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
- <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
- <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
- <SortedAscendingCellStyle BackColor="#FDF5AC" />
- <SortedAscendingHeaderStyle BackColor="#4D0000" />
- <SortedDescendingCellStyle BackColor="#FCF6C0" />
- <SortedDescendingHeaderStyle BackColor="#820000" />
- </asp:GridView>
- <asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
- SelectMethod="GetEmployees" TypeName="EmployeeBLL"></asp:ObjectDataSource>
- </div>
FormView
- <h1>Employee List FormView Example</h1>
- <div>
- <asp:Panel ID="Panel1" runat="server" ForeColor="Green">
- <asp:FormView
- ID="FormView1"
- runat="server"
- DataSourceID="ObjectDataSource1"
- AllowPaging="true">
- <ItemTemplate>
- <b>Employee ID: </b>
- <%# Eval("EmployeeID") %>
- <br />
- <b>Name: </b>
- <%# Eval("LastName") %> <%# Eval("FirstName") %>
- <br />
- <b>BirthDate: </b>
- <%# Eval("BirthDate")%>
- <br />
- <b>City: </b>
- <%# Eval("City") %>
- <br />
- <b>Region: </b>
- <%# Eval("Region")%>
- <br />
- <b>PostalCode: </b>
- <%# Eval("PostalCode")%>
- <br />
- <b>Country: </b>
- <%# Eval("Country") %>
- </ItemTemplate>
- </asp:FormView>
- </asp:Panel>
- </div>
Hit F5 to see the output.
Image 5.
Image 6.
For more information, download the attached sample application with the database.