Mega Code Archive

 
Categories / ASP.Net Tutorial / Data Binding
 

Data binding by filling list and drop-down list controls with the results of direct ADO NET queries

<%@ 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>DataSource-based Binding</title> </head> <body>     <div id="pageContent">         <form id="form1" runat="server">             <h2>Browse Customers</h2>             <hr />             <table><tr>                 <td valign="top">                      <asp:ListBox ID="CustomerList" runat="server" Height="200px" Width="280px" />                 </td>                 <td valign="top">                      <asp:DropDownList runat="server" ID="CountryList" AppendDataBoundItems="True" Width="130px">                         <asp:ListItem Text="[All]" />                      </asp:DropDownList>                                 <br />                     <asp:Button ID="CountriesButton" runat="server" Text="Get countries..." Width="130px"                         OnClick="CountriesButton_Click" />                 </td>             </tr></table>             <hr />             <asp:Button ID="CustomersButton" runat="server" Text="Get customers..."                  OnClick="CustomersButton_Click" />        </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.UI; using System.Web.UI.WebControls; public partial class Default : System.Web.UI.Page {     protected void CountriesButton_Click(object sender, EventArgs e)     {         if (CountryList.Items.Count > 1)             return;         string connString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;         using (SqlConnection conn = new SqlConnection(connString))         {             string cmdText = "SELECT DISTINCT country FROM customers";             SqlCommand cmd = new SqlCommand(cmdText, conn);             cmd.Connection.Open();             using (SqlDataReader reader = cmd.ExecuteReader())             {                 CountryList.DataSource = reader;                 CountryList.DataTextField = "country";                 CountryList.DataBind();             }         }     }     protected void CustomersButton_Click(object sender, EventArgs e)     {         CustomerList.Items.Clear();         string connString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;         string cmdText1 = "SELECT CustomerID, CompanyName, Country FROM customers";         string cmdText2 = "SELECT CustomerID, CompanyName, Country FROM customers WHERE country='{0}'";         string cmdText = cmdText1;         if (CountryList.SelectedIndex > 0)             cmdText = String.Format(cmdText2, CountryList.SelectedValue);         DataSet data = new DataSet();         SqlDataAdapter adapter = new SqlDataAdapter(cmdText, connString);         adapter.Fill(data);         CustomerList.DataMember = "Table";         CustomerList.DataTextField = "companyname";         CustomerList.DataValueField = "customerid";         CustomerList.DataSource = data;         CustomerList.DataBind();     } }