PDA

Click to See Complete Forum and Search --> : Selection box connected to a database


hpl
Dec 6th, 2002, 07:57 AM
How can I generate the content of a selection box from a database e.g an Access database?

pvb
Dec 8th, 2002, 04:51 AM
here's one way:

<%@ Import Namespace = "System.Data" %>
<%@ Import Namespace = "System.Data.OleDb" %>
<script runat="server" language="vb">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
If Not Page.IsPostBack Then
bindAuthors()
End If
End Sub
Protected Sub bindAuthors()
Dim mdbPath As String = "D:\AccessData\Biblio2002.mdb"
Dim connString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & mdbPath
Dim cn As OleDbConnection = New OleDbConnection(connString)
Dim cmdText As String = "Select au_id As AuthorId, Author From Authors Order By Author"
Dim cmd as OleDbCommand = New OleDbCommand(cmdText, cn)
cmd.Connection.Open()
Authors.DataSource = cmd.ExecuteReader()
Authors.DataValueField = "AuthorId"
Authors.DataTextField = "Author"
Authors.DataBind()
cmd.Connection.Close()
End Sub
</script>
<html>
<head>
<title>DataBoundCombo</title>
</head>
<body>

<form id="DataBoundCombo" method="post" runat="server">
<asp:DropDownList
Runat="server"
ID="Authors"/>
</form>

</body>
</html>