一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

asp.net 读取数据库内容绑定到控件上的实例

时间:2011-06-02 编辑:简简单单 来源:一聚教程网

asp教程.net 读取数据库教程内容绑定到控件上的实例

 

<%@ Page language="c#" src="AuthorBrowser.aspx.cs" AutoEventWireup="false" Inherits="AuthorBrowser" %>

 
   


      Select Author:

 


<%--
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;

  public class AuthorBrowser : System.Web.UI.Page
  {
    protected System.Web.UI.WebControls.Label Label1;
    protected System.Web.UI.WebControls.Label lblResults;
    protected System.Web.UI.WebControls.DropDownList lstAuthor;
 
    private string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0; data source=" + System.Web.HttpContext.Current.Server.MapPath("EmployeeDatabase.mdb");

    private void Page_Load(object sender, System.EventArgs e)
    {
      if (!this.IsPostBack)
      {
        FillAuthorList();
      }
    }
      
    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
      InitializeComponent();
      base.OnInit(e);
    }
   
    private void InitializeComponent()
    {   
      this.lstAuthor.SelectedIndexChanged += new System.EventHandler(this.lstAuthor_SelectedIndexChanged);
      this.Load += new System.EventHandler(this.Page_Load);

    }
    #endregion

    private void FillAuthorList()
    {
      lstAuthor.Items.Clear();

      // Define the Select statement.
      // Three pieces of information are needed: the unique id,
      // and the first and last name.
      string selectSQL;
      selectSQL = "SELECT FirstName, LastName, ID FROM Employee";

      // Define the ADO.NET objects.
      OleDbConnection con = new OleDbConnection(connectionString);
      OleDbCommand cmd = new OleDbCommand(selectSQL, con);
      OleDbDataReader reader;

      // Try to open database and read information.
      try
      {
        con.Open();
        reader = cmd.ExecuteReader();

        // For each item, add the author name to the displayed
        // list box text, and store the unique ID in the Value property.
        while (reader.Read())
        {
          ListItem newItem = new ListItem();
          newItem.Text = reader["LastName"] + ", " + reader["FirstName"];
          newItem.Value = reader["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();
      }
    }

    private void lstAuthor_SelectedIndexChanged(object sender, System.EventArgs e)
    {
      // Create a Select statement that searches for a record
      // matching the specific author id from the Value property.
      string selectSQL;
      selectSQL = "SELECT * FROM Employee ";
      selectSQL += "WHERE ID=" + lstAuthor.SelectedItem.Value + "";

      // Define the ADO.NET objects.
      OleDbConnection con = new OleDbConnection(connectionString);
      OleDbCommand cmd = new OleDbCommand(selectSQL, con);
      OleDbDataReader reader;

      // Try to open database and read information.
      try
      {
        con.Open();
        reader = cmd.ExecuteReader();
        reader.Read();
        lblResults.Text = "" + reader["LastName"];
        lblResults.Text += ", " + reader["FirstName"] + "

";
        lblResults.Text += "ID: " + reader["ID"] + "
";
        reader.Close();
      }
      catch (Exception err)
      {
        lblResults.Text = "Error getting author. ";
        lblResults.Text += err.Message;
      }
      finally
      {
        con.Close();
      }

    }
  }

--%>
          


      

热门栏目