Creating an ASP.NET VB Version database-driven dropdown menu with SQL Server is easy with the following code.
Live Lessons Example - ASP.NET (VB Version) Database Driven Select Menu (Dropdown Menu)
Not much coding is involved in this project. Unlike the original code, this uses [SQL Server ] instead of an [Access database ].
(Copy and paste are available on the Source Code page)
We will create our database table and load the data for this project. Create a connection to our database for use in our project. Show how to close the RecordSet and Database connection. Create a SELECT statement to populate our select menu. Demonstrate how to split up files for better code organization. [web.config]
CFFCS |
|
ASP.NET (VB/C#)
Expand
Copy
Download Code
<?xml version="1.0"?>
<!--
For more information on how to configure your ASP.NET application, please visit
https://go.microsoft.com /fwlink/?LinkId=169433
-->
<configuration>
<connectionStrings configSource="database.config " />
<system.web >
<compilation debug="true" strict="false" explicit="true" targetFramework="4.7.2" />
<httpRuntime targetFramework="4.7.2" />
</system.web >
</configuration>
[database.config"]
CFFCS |
|
ASP.NET (VB/C#)
Expand
Copy
Download Code
<?xml version="1.0"?>
<connectionStrings>
<add name ="Virtual-Learning" connectionString="Data Source=sqlcorecs-01\sql2019;Database=Virtual-Class -01;User ID=testuser;Password=testuser;MultipleActiveResultSets=True"/>
</connectionStrings>
[Load.aspx]
CFFCS |
|
ASP.NET (VB/C#)
Expand
Copy
Download Code
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Load.aspx.vb " Inherits ="Load" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org /1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<strong>ASP.NET (VB Version) SQL Server Database Driven Dropdown Menu</strong>
This demonstration loads all values from a database table into a Select Menu.
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
</div>
</form>
</body>
</html>
[Load.aspx.vb]
CFFCS |
|
ASP.NET (VB/C#)
Expand
Copy
Download Code
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
Partial Class Load
Inherits System.Web.UI .Page
Protected Sub Page_Load(sender As Object , e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim constr As String = ConfigurationManager.ConnectionStrings ("Virtual-Learning").ConnectionString
Using con As New SqlConnection (constr)
Using cmd As New SqlCommand("SELECT ddId, ddName FROM Dropdown")
cmd.CommandType = CommandType.Text
cmd.Connection = con
con.Open ()
DropDownList1.DataSource = cmd.ExecuteReader ()
DropDownList1.DataTextField = "ddName"
DropDownList1.DataValueField = "ddId"
DropDownList1.DataBind ()
con.Close ()
End Using
End Using
DropDownList1.Items.Insert (0, New ListItem("--Select Programming Language--", "0"))
End If
End Sub
End Class
Other Articles Related to this Entry.