ASP.Net/ADO.net Database/Query

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Read data from database in asp dropdownlist selection changed event (C#)

<%@ Page language="c#" src="AuthorBrowser.aspx.cs" AutoEventWireup="false" Inherits="AuthorBrowser" %>
<HTML>
  <body>
    <form id="Form1" method="post" runat="server">
      <asp:label id="Label1" style="Z-INDEX: 105; LEFT: 32px; POSITION: absolute; TOP: 24px" runat="server" Font-Size="X-Small" Font-Names="Verdana" Width="181px" Height="20px"> Select Author:</asp:label><asp:label id="lblResults" style="Z-INDEX: 106; LEFT: 32px; POSITION: absolute; TOP: 64px" runat="server" Font-Size="X-Small" Font-Names="Verdana" Width="384px" Height="168px"></asp:label><asp:dropdownlist id="lstAuthor" style="Z-INDEX: 102; LEFT: 184px; POSITION: absolute; TOP: 20px" runat="server" Font-Size="X-Small" Font-Names="Verdana" Width="171px" Height="22px" AutoPostBack="True"></asp:dropdownlist></form>
  </body>
</HTML>

<%--
using System;
using System.Collections;
using System.ruponentModel;
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 = "<b>" + reader["LastName"];
        lblResults.Text += ", " + reader["FirstName"] + "</b><br>";
        lblResults.Text += "ID: " + reader["ID"] + "<br>";
        reader.Close();
      }
      catch (Exception err)
      {
        lblResults.Text = "Error getting author. ";
        lblResults.Text += err.Message;
      }
      finally
      {
        con.Close();
      }
    }
  }
--%>


<A href="http://www.nfex.ru/Code/ASPDownload/EmployeeDatabase.zip">EmployeeDatabase.zip( 10 k)</a>


Read data from database in code behind (C#)

<%@ Page language="c#" src="AuthorBrowser.aspx.cs" AutoEventWireup="false" Inherits="AuthorBrowser" %>
<HTML>
  <body>
    <form id="Form1" method="post" runat="server">
      <asp:label id="Label1" style="Z-INDEX: 105; LEFT: 32px; POSITION: absolute; TOP: 24px" runat="server" Font-Size="X-Small" Font-Names="Verdana" Width="181px" Height="20px"> Select Author:</asp:label><asp:label id="lblResults" style="Z-INDEX: 106; LEFT: 32px; POSITION: absolute; TOP: 64px" runat="server" Font-Size="X-Small" Font-Names="Verdana" Width="384px" Height="168px"></asp:label><asp:dropdownlist id="lstAuthor" style="Z-INDEX: 102; LEFT: 184px; POSITION: absolute; TOP: 20px" runat="server" Font-Size="X-Small" Font-Names="Verdana" Width="171px" Height="22px" AutoPostBack="True"></asp:dropdownlist></form>
  </body>
</HTML>

<%--
using System;
using System.Collections;
using System.ruponentModel;
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 = "<b>" + reader["LastName"];
        lblResults.Text += ", " + reader["FirstName"] + "</b><br>";
        lblResults.Text += "ID: " + reader["ID"] + "<br>";
        reader.Close();
      }
      catch (Exception err)
      {
        lblResults.Text = "Error getting author. ";
        lblResults.Text += err.Message;
      }
      finally
      {
        con.Close();
      }
    }
  }
--%>


<A href="http://www.nfex.ru/Code/ASPDownload/EmployeeDatabase.zip">EmployeeDatabase.zip( 10 k)</a>