Displaying Data
New to .NET is data binding: a method of attaching tables of data to user interface elements. Using data binding, you can query results from a
SQL Server database, and "bind" the results to a drop down box. ASP.NET will handle the HTML code, so there is no longer a need, as there was in
classic ASP, for writing Response.Write "<option>"... etc.
Binding Data
Binding data to a control is simply a matter of setting the control's DataSource property to a data source, and calling the control's DataBind method. The following
ASP.NET page queries a database, and then binds a list of Authors to a DataGrid control.
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Data" %>
<script runat="server">
void Page_Load(Object sender, EventArgs e)
{
   SqlConnection myConnection;
   SqlDataAdapter myAdapter;
   string CmdStr;
   string ConnStr;
   DataSet ds = new DataSet();
       ConnStr = "server=localhost;uid=sa;pwd=;database=pubs";
   myConnection = new SqlConnection(ConnStr);
       CmdStr = "select * from authors order by au_lname";
   myAdapter = new SqlDataAdapter(CmdStr, myConnection);
   myAdapter.Fill(ds, "Authors");
       ArticleGrid.DataSource = ds.Tables["Authors"].DefaultView;
   ArticleGrid.DataBind();
}
</script>
<html>
<body>
 <h1>Authors</h1>
 <asp:DataGrid id="ArticleGrid" runat="server" width="100%" />
</body>
</html>