ADO Net Database ASP.Net Tutorial

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



    Untitled Page


    
    

                           runat="server" 
                   Width="120px" 
                   Height="20px">Select Author: 
                          runat="server" 
                      Width="256px" 
                      Height="22px" 
                      AutoPostBack="True" 
                      onselectedindexchanged="lstAuthor_SelectedIndexChanged">
    
        
    
    
  

    


File: Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Configuration;
using System.Data.SqlClient;
using System.Text;
public partial class AuthorBrowser : System.Web.UI.Page
{
    private string connectionString = WebConfigurationManager.ConnectionStrings["Pubs"].ConnectionString;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            FillAuthorList();
        }
    }
    private void FillAuthorList()
    {
        lstAuthor.Items.Clear();
        string selectSQL = "SELECT au_lname, au_fname, au_id FROM Authors";
        SqlConnection con = new SqlConnection(connectionString);
        SqlCommand cmd = new SqlCommand(selectSQL, con);
        SqlDataReader reader;
        try
        {
            con.Open();
            reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                ListItem newItem = new ListItem();
                newItem.Text = reader["au_lname"] + ", " + reader["au_fname"];
                newItem.Value = reader["au_id"].ToString();
                lstAuthor.Items.Add(newItem);
            }
            reader.Close();
        }
        catch (Exception err)
        {
            lblResults.Text = "Error reading list of names. ";
            lblResults.Text += err.Message;
        }
        finally
        {
            con.Close();
        }
    }
    protected void lstAuthor_SelectedIndexChanged(object sender, EventArgs e)
    {
        string selectSQL;
        selectSQL = "SELECT * FROM Authors ";
        selectSQL += "WHERE au_id='" + lstAuthor.SelectedItem.Value + "'";
        SqlConnection con = new SqlConnection(connectionString);
        SqlCommand cmd = new SqlCommand(selectSQL, con);
        SqlDataReader reader;
        try
        {
            con.Open();
            reader = cmd.ExecuteReader();
            reader.Read();
            StringBuilder sb = new StringBuilder();
            sb.Append("");
            sb.Append(reader["au_lname"]);
            sb.Append(", ");
            sb.Append(reader["au_fname"]);
            sb.Append("
");
            sb.Append("Phone: ");
            sb.Append(reader["phone"]);
            sb.Append("");
            sb.Append("Address: ");
            sb.Append(reader["address"]);
            sb.Append("");
            sb.Append("City: ");
            sb.Append(reader["city"]);
            sb.Append("");
            sb.Append("State: ");
            sb.Append(reader["state"]);
            sb.Append("");
            lblResults.Text = sb.ToString();
            reader.Close();
        }
        catch (Exception err)
        {
            lblResults.Text = "Error getting author. ";
            lblResults.Text += err.Message;
        }
        finally
        {
            con.Close();
        }
    }
}
File: Web.config