Mega Code Archive

 
Categories / ASP.Net Tutorial / ADO Net Database
 

Create and display in-memory calculated fields

<%@ 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();     } }