Populate a Dropdown List box using VB.NET and Code-behind page

Contributed by
Rating: 4 stars4 stars4 stars4 stars4 stars / 39
September 23, 2001
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

Populate a Dropdown using VB.NET and Code-behind

This simple example demonstrates how to populate a dropdown box using a code-behind page.  This is one method of separating presentation from business logic.

 

ASPX page

<%@ Page Language="VB" debug="true" Inherits="myCodeBehind" src="2.vb"%>
<html>
<title>Fill dropdown with VB, Sql DataReader & Code Behind</title>
</head>
    <body>
    <asp:Dropdownlist id="atMyDropDownList" DataValueField = "au_lname" DataTextField = "au_id" runat="server" />
    </body>
</html>

Code behind form

Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration

public class myCodeBehind : Inherits System.Web.UI.Page

    public atMyDropDownList as DropDownList

    private Sub Page_Load(Sender as object, e as System.EventArgs)
        dim sqlText as string = "select * from authors"
        atMyDropDownList.DataSource = GetDr( sqlText )
        atMyDropDownList.DataBind()
    End Sub

    private Function GetDr(sqlText as string) as SqlDataReader
        dim dr as SqlDataReader
        dim sqlConn as SqlConnection = new SqlConnection(ConnectionString())
        dim sqlCmd as SqlCommand = new SqlCommand(sqlText,sqlConn)
        sqlCmd.Connection.Open()
        dr = sqlCmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
        return dr
    End Function

    private Function ConnectionString() as string
            dim myConn as string = ConfigurationSettings.AppSettings("DSN_pubs")
            return myConn
    End Function

    private Sub Page_Init(Sender as object, e as System.EventArgs)
                   
    End Sub

End Class

Web.Config

<configuration>
    <appSettings>
        <add key="DSN_NorthWind" value="server=(local)\NetSDK;database=NorthWind;Trusted_Connection=yes" />
        <add key="DSN_pubs" value="server=(local)\NetSDK;database=pubs;Trusted_Connection=yes" />
    </appSettings>
</configuration>

blog comments powered by Disqus
ASP CODE ARTICLES

- ASP Forms
- ASP: The Beginning
- Getting Remote Files With ASP Continued
- Inbox and Outbox Manipulation in ASP
- Relational DropDownList Using VB.NET
- Ad Tracking URL Hits
- Use ViewState to display one record per page...
- Send Email using ASP.NET formatted in HTML
- ASP File Explorer
- ASP/XML Interview questions by Srivatsan Sri...
- Pressing RETURN won't submit the form
- This shows how you get the TEXT of a combo r...
- Group Data by Adrian Forbes
- Multiple checkbox select sample
- Multiple checkbox select with all values sam...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 9 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials