ASP.net Dropdown List

liunx

Guest
:(
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.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 DataValueFieldCipher , thank alot for your helpHI , 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 alottry 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()Thank for your kind help aish :)
 
Back
Top