ObjectDataSource - Populate FormView
FormViewObjectDataSourceExample.aspx
<%@ Page Language="C#" %>
<!DOCTYPE html>
<script runat="server">
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>asp.net ObjectDataSource example: populate FormView</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:Navy">FormView ObjectDataSource Example</h2>
<asp:ObjectDataSource
ID="ObjectDataSource1"
runat="server"
TypeName="EmployeeList"
SelectMethod="GetEmployees"
>
</asp:ObjectDataSource>
<asp:Panel ID="Panel1" runat="server" ForeColor="Crimson" Font-Italic="true">
<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>Title: </b>
<%# Eval("Title") %>
<br />
<b>City: </b>
<%# Eval("City") %>
<br />
<b>Country: </b>
<%# Eval("Country") %>
</ItemTemplate>
</asp:FormView>
</asp:Panel>
</div>
</form>
</body>
</html>
EmployeeList.cs
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
public class EmployeeList
{
public EmployeeList(){}
public DataSet GetEmployees()
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = WebConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
SqlCommand myCommand = new SqlCommand();
myCommand.CommandText = "SELECT EmployeeID, LastName, FirstName, Title, City, Country FROM Employees";
myCommand.Connection = conn;
SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand);
DataSet myDataSet = new DataSet();
myAdapter.Fill(myDataSet);
return myDataSet;
}
}