ASP tutorial. NET reading database tutorial content binding to instances on a control
<%@ Page language= "C #" src= "AuthorBrowser.aspx.cs" autoeventwireup= "false" inherits= "Authorbrowser"%>
<BODY>
<form id= "Form1" method= "POST" runat= "Server"
& nbsp; <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>
<%--
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 and the 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 to a record
Matching the specific author ID from the Value.
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 ();
}
}
}
--%>