Click to See Complete Forum and Search --> : ASP.net Dropdown List


Bruce Lim
11-18-2005, 01:09 AM
:(
Hi everyone, i have just started programming on ASP.net. I have encountered problems coding. Can anyone show me an example on how to code a drop down list extracting from the database? The code must be written under *.aspx.vb and return the dropdown list under *.aspx

I am using visual Studio.net 2003 for coding.

Here is the scenario;
1) The Database connection and SQL statement must be written under *.aspx.vb. The SQL statement will extract the the fields under the tblDepartment.

2) The *.aspx form will display the result of extracted field and list them under the dropdown list menu


Can anyone show me an example on the scenario above?

Thank you for your help.

Cipher
11-18-2005, 04:50 AM
Here's an easy exaple i stole from uleashed book
<%@ Import Namespace="System.Data.SqlClient" %>

<Script Runat="Server">

Sub Page_Load
If Not IsPostBack Then
Dim conPubs As SqlConnection
Dim cmdSelect As SqlCommand
Dim dtrAuthors As SqlDataReader

conPubs = New SqlConnection( "Server=localhost;UID=sa;PWD=secret;Database=Pubs" )
conPubs.Open()
cmdSelect = New SqlCommand( "Select au_lname From Authors", conPubs )
dtrAuthors = cmdSelect.ExecuteReader()

dropAuthors.DataSource = dtrAuthors
dropAuthors.DataTextField = "au_lname"
dropAuthors.DataBind()

dtrAuthors.Close()
conPubs.Close()
End If
End Sub

</Script>

<html>
<head><title>DropDownList.aspx</title></head>
<body>
<form Runat="Server">

<asp:DropDownList
ID="dropAuthors"
Runat="Server" />

</form>
</body>
</html>
Note: you can assgin different Text and Value by using DataValueField

Bruce Lim
11-18-2005, 07:52 AM
Cipher , thank alot for your help

Bruce Lim
11-20-2005, 08:39 PM
HI , i have a small problem. How do i execute a SQL INSERT statement.

For example, in ASP 3.0 we have objconn.Execute (StrSQL)

Where objConn is the ADODB.Connection and StrSQL is "INSERT INTO tblDepartment (id,name) VALUES '1234',''ranger'"

Can anyone help me? Thank alot

aish
11-20-2005, 10:48 PM
try this code.


Dim conPubs As SqlConnection
Dim cmdInsert As SqlCommand
conPubs = New SqlConnection( "Server=localhost;UID=sa;PWD=secret;Database=Pubs" )
conPubs.Open()
cmdInsert = New SqlCommand( "insert into stores values ('8041','Bookbeat','679 Carson St.','Portland','OR','89076')", conPubs )
cmdInsert.ExecuteNonQuery()
conPubs.Close()

Bruce Lim
11-21-2005, 12:10 AM
Thank for your kind help aish :)