To Examples
Groceries3 Example, source code file groceries3.aspx
<%@ Page Language="C#" %> <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.OleDb" %> <!-- Groceries3 Example Populate the DropDownList with the IDs from the database table. Then use these IDs to look up further information from the table. --> <script runat="server"> public void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { // Create and configure connection string. OleDbConnection c = new OleDbConnection(); c.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=e:/ectserver/sjost/Database/groceries.accdb;"; // Create Command object with query from drop down list. OleDbCommand sc = new OleDbCommand("SELECT Code FROM Groceries;", c); // Create DataAdapter object. OleDbDataAdapter da = new OleDbDataAdapter(sc); // Create and populate DataSet object. DataSet ds = new DataSet(); int n = da.Fill(ds, "Groceries"); // Display data in Literal control. foreach (DataRow r in ds.Tables["Groceries"].Rows) { ddlCode.Items.Add(r.ItemArray[0].ToString()); } } } protected void ddlCode_SelectedIndexChanged(object sender, EventArgs e) { // Create and configure connection string. OleDbConnection c = new OleDbConnection(); c.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=e:/ectserver/sjost/Database/groceries.accdb;"; // Create Command object with query from drop down list. string code = ddlCode.SelectedItem.ToString(); OleDbCommand sc = new OleDbCommand( "SELECT * FROM Groceries WHERE code=" + code + ";", c); // Create DataAdapter object. OleDbDataAdapter da = new OleDbDataAdapter(sc); // Create and populate DataSet object. DataSet ds = new DataSet(); int n = da.Fill(ds, "Groceries"); // Copy values from dataset to controls. if (ds.Tables[0].Rows.Count == 1) { txtItem.Text = ds.Tables[0].Rows[0].ItemArray[0].ToString(); txtQuantity.Text = ds.Tables[0].Rows[0].ItemArray[2].ToString(); txtPrice.Text = ds.Tables[0].Rows[0].ItemArray[3].ToString(); txtOnSale.Text = ds.Tables[0].Rows[0].ItemArray[4].ToString(); } else { txtItem.Text = ""; } } </script> <html> <head> <title>Groceries3 Example</title> <link rel="stylesheet" class="text/css" href="../examples.css" /> <style type="text/css"> td { padding:0.15cm; } .r { text-align: right; } </style> </head> <body> <h2>Groceries3 Example</h2> <p>Select code of grocery item to retrieve from database. Copy other fields from dataset to textboxes.</p> <form id="frmSelect" runat="server"> <p>Code <asp:DropDownList ID="ddlCode" runat="server" onselectedindexchanged="ddlCode_SelectedIndexChanged" AutoPostBack="True" CssClass="ctrl"> </asp:DropDownList></p> <hr /> <br /> <table> <tr> <td class="r">Item</td> <td><asp:TextBox ID="txtItem" runat="server" CssClass="ctrl" /></td> </tr> <tr> <td class="r">Quantity</td> <td><asp:TextBox ID="txtQuantity" runat="server" CssClass="ctrl" /></td> </tr> <tr> <td class="r">Price</td> <td><asp:TextBox ID="txtPrice" runat="server" CssClass="ctrl" /></td> </tr> <tr> <td class="r">OnSale</td> <td><asp:TextBox ID="txtOnSale" runat="server" CssClass="ctrl" /></td> </tr> </table> </form> </body> </html>