Just copy and paste this code into a new blank page. This is example uses MS Access. Just change the location of the database on your computer to use this code. If you want to use the pubs database with SQL Server, change the whole Provider stringconnection, command and the imported namespace.
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<html>
<head>
<meta name="GENERATOR" Content="ASP Express 2.0">
<title>Dropdown List</TITLE>
<Link REL=STYLESHEET HREF="/basicArial.css" TYPE="text/css">
<script language="VB" runat="server">
Sub Page_Load(Source as Object, E as EventArgs)
If not IsPostBack then
BindData()
end if
End Sub
Sub button1_click(Source as Object, E as EventArgs)
label1.text=" You selected " & MyList.selecteditem.text
End Sub
Sub Reset_Click(Source as Object, E as EventArgs)
label1.text=""
BindData()
End Sub
Sub BindData()
Dim strSQL as string
Dim strConn as string
strSQL="Select distinct city from publishers where city <>”"
strConn ="PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & server.mappath("/biblio.mdb") & ";"
Dim Conn as New OLEDBConnection(strConn)
Dim Cmd as New OLEDBCommand(strSQL,Conn)
Conn.Open()
MyList.DataSource = Cmd.ExecuteReader(system.data.CommandBehavior.CloseConnection)
MyList.DataBind()
Conn.Close()
End Sub
</script>
</head>
<div align="center"><form runat="server">
<asp:dropdownlist id="MyList" datatextfield="city" runat="server"/>
<asp:Button id="button1" Text="Submit" onclick="button1_click" runat="server" />
<asp:Button id="Reset" Text="Reset" onclick="Reset_Click" runat="server" /> <p>
</form>
<b><font Color="#800000"><asp:Label ID="label1" runat="server" /></font></b> </div>
</body>
</html>