Populate a DropDownList reading data from Access
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<html>
<script language="C#" runat="server">
protected void Page_Load ( object src, EventArgs e ) {
OleDbConnection myConn = new OleDbConnection(
"Provider=Microsoft.Jet.OleDb.4.0; Data Source=" +
Server.MapPath("database/utenti.mdb"));
OleDbCommand myCmd = new OleDbCommand("select Nome, Cognome from utenti", myConn);
if (!IsPostBack)
{
try
{
myConn.Open();
myDropDownList.DataSource = myCmd.ExecuteReader();
myDropDownList.DataValueField="nome";
myDropDownList.DataTextField="cognome";
myDropDownList.DataBind();
}
finally
{
myConn.Close();
}
}
}
</script>
<body>
<form Name="Form1" Runat="server" ID="Form1">
<asp:DropDownList ID="myDropDownList" Runat="server"></asp:DropDownList>
</form>
</body>
</html>
</html>
Download Code...
Print Page