Creating an ASP.NET C# Version database-driven dropdown menu with SQL Server is easy with the following code.
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. Live Lessons Example - ASP.NET (C# Version) Database Driven Select Menu (Dropdown Menu)
[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="C#" AutoEventWireup="true" CodeFile="Load.aspx.cs " Inherits ="Load" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org /1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<strong>ASP.NET (C# Version) SQL Server Database Driven Dropdown Menu</strong>
This demonstration loads all values into a Select Menu from a database table.
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
</div>
</form>
</body>
</html>
[Load.aspx.cs]
CFFCS |
|
ASP.NET (VB/C#)
Expand
Copy
Download Code
using System;
using System.Collections.Generic ;
using System.Linq ;
using System.Web ;
using System.Web.UI ;
using System.Web.UI.WebControls ;
using System.Data ;
using System.Configuration ;
using System.Data.SqlClient ;
public partial class Load : System.Web.UI .Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack )
{
string constr = ConfigurationManager.ConnectionStrings ["Virtual-Learning"].ConnectionString;
using (SqlConnection con = new SqlConnection (constr))
{
using (SqlCommand cmd = 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 ();
}
}
DropDownList1.Items.Insert (0, new ListItem("--Select Programming Language--", "0"));
}
}
}
Other Articles Related to this Entry.