Create and display in-memory calculated fields : DataTable « ADO.net Database « ASP.NET Tutorial






<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" 
    Inherits="Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Cities and Customers</title>
</head>
<body>

    <div id="pageContent">
        <form id="form1" runat="server">
            <h2>Find Customers' Cities</h2>
            <hr />

            <asp:DropDownList runat="server" ID="CityList" Width="230px">
            </asp:DropDownList>            

            <asp:Button ID="CityButton" runat="server" Text="Get cities..." OnClick="CityButton_Click" />
            <hr />
        </form>
    </div>
</body>
</html>

File: Default.aspx.cs

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;


public partial class Default : System.Web.UI.Page
{
    protected void CityButton_Click(object sender, EventArgs e)
    {
        string connString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
        string cmdText = "SELECT DISTINCT country, city FROM customers";

        DataTable data = new DataTable();
        SqlDataAdapter adapter = new SqlDataAdapter(cmdText, connString);
        adapter.Fill(data);
        data.Columns.Add("CityCountry", typeof(string), "city + '   ('+ country + ')'");

        CityList.DataTextField = "CityCountry";
        CityList.DataSource = data;
        CityList.DataBind();
    }
}








18.29.DataTable
18.29.1.The DataTable object represents an in-memory database table.
18.29.2.Create and display in-memory calculated fields
18.29.3.How to load a DataTable from a DataReader (C#)
18.29.4.How to load a DataTable from a DataReader (VB)
18.29.5.Build a DataTable