Home
About
Contact
Categories
Classic ASP (32 - Entries)
CSS (1 - Entry)
JavaScript (5 - Entries)
Databases (30 - Entries)
ASP.NET (26 - Entries)
Delphi (6 - Entries)
Windows Server Core (12 - Entries)
VMWare (1 - Entry)
Code Editing Tools (2 - Entries)
Linux (4 - Entries)
Dell Servers (15 - Entries)
Design and Editing Software (0 - Entries)
Blog Entries
2025 (3 - Entries)
Bug Reports
(Bugs Fixed
CFFCS Coding Source
Please report any errors to the [
Contact
] page. Thank you.
Classic ASP (32)
CSS (1)
JavaScript (5)
Databases (30)
ASP.NET (26)
Delphi (6)
Windows Server Core (12)
VMWare (1)
Code Editing Tools (2)
Linux (4)
Dell Servers (15)
Design and Editing Software (0)
Tools
Format Your SQL Script
Minify your CSS
Resources
[View The Source Code For This Project]
ASP.NET
C#
ASP.NET (C#) Fill select menu with records from a database, insert selected records with live updated data
Live Editing Disabled for Server-Side Example
HTML
Load.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Load.aspx.cs" Inherits="Load" %> <% String getMyID = Request.QueryString["MyID"]; %>
<%if (getMyID != null && getMyID != string.Empty) {%>
ASP.NET (C# Version) SQL Server Database Driven Dropdown Menu
This demonstration populates a Select Menu with values from a database table.
" />
<%} else { %>
Load ID
to proceed <% }%>
JavaScript
Newajaxsbmt.js
$(document).ready(function(){ $('form').change(function(e){ e.preventDefault(); $.ajax({ url: "Update.ashx", type: "POST", data: $(this).serialize(), success: function(data){ $(".postData").html(data); }, error: function(){ $(".postData").html("Form submission failed!"); } }); }); });
SQL
DropDown
-- Create a new database called [Virtual-Class-01] in SQL Server. -- Right-click and choose [New Query] -- Copy and paste the code below and hit [Execute] USE [Virtual-Class-01] GO /****** Object: Table [dbo].[DropDown] Script Date: 8/4/2022 10:40:41 PM ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[DropDown]( [ddID] [int] IDENTITY(1,1) NOT NULL, [ddName] [nvarchar](50) NOT NULL, CONSTRAINT [PK_DropDown] PRIMARY KEY CLUSTERED ( [ddID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY] ) ON [PRIMARY] GO /****** Object: Table [dbo].[Dropdown2] Script Date: 8/4/2022 10:40:41 PM ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[Dropdown2]( [InsertID] [int] IDENTITY(1,1) NOT NULL, [MyID] [int] NOT NULL, [ddid] [int] NOT NULL, CONSTRAINT [PK_Dropdown2] PRIMARY KEY CLUSTERED ( [InsertID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY] ) ON [PRIMARY] GO SET IDENTITY_INSERT [dbo].[DropDown] ON GO INSERT [dbo].[DropDown] ([ddID], [ddName]) VALUES (1, N'asp classic') GO INSERT [dbo].[DropDown] ([ddID], [ddName]) VALUES (2, N'visual basic') GO INSERT [dbo].[DropDown] ([ddID], [ddName]) VALUES (3, N'jquery') GO INSERT [dbo].[DropDown] ([ddID], [ddName]) VALUES (4, N'javascript') GO INSERT [dbo].[DropDown] ([ddID], [ddName]) VALUES (5, N'asp.net') GO INSERT [dbo].[DropDown] ([ddID], [ddName]) VALUES (6, N'vb.net') GO INSERT [dbo].[DropDown] ([ddID], [ddName]) VALUES (7, N'C#') GO INSERT [dbo].[DropDown] ([ddID], [ddName]) VALUES (8, N'web.config') GO INSERT [dbo].[DropDown] ([ddID], [ddName]) VALUES (9, N'database.config') GO INSERT [dbo].[DropDown] ([ddID], [ddName]) VALUES (10, N'runtime') GO SET IDENTITY_INSERT [dbo].[DropDown] OFF GO
ASP.NET
web.config
database.config
Load.aspx.cs
LoadIt.ashx
Update.ashx
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; protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { string cn = ConfigurationManager.ConnectionStrings["Virtual-Learning"].ConnectionString; using (SqlConnection con = new SqlConnection(cn)) { 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")); } }
<%@ WebHandler Language="C#" Class="LoadIt" %> using System; using System.Web; using System.Data; using System.Configuration; using System.Data.SqlClient; public void ProcessRequest (HttpContext context) { string getID = context.Request["DropDownList1"]; string getMyID = context.Request["MyID"]; //context.Response.Write(getID + "-" + getMyID); var LessonCon = new SqlConnection(); SqlCommand LessonCMD = new SqlCommand(); LessonCon = new SqlConnection(ConfigurationManager.ConnectionStrings["Virtual-Learning"].ConnectionString); LessonCon.Open(); SqlDataReader rsDD; SqlCommand getDD = new SqlCommand("SELECT DropDown.ddID, Dropdown2.MyID, DropDown.ddName FROM DropDown INNER JOIN Dropdown2 ON DropDown.ddID = Dropdown2.ddid WHERE Dropdown2.MyID = @MyID", LessonCon); getDD.Parameters.Add(new SqlParameter("@MyID", getMyID)); rsDD = getDD.ExecuteReader(); while (rsDD.Read()) { context.Response.Write(rsDD["ddID"] + "-" + rsDD["ddName"] + "
"); } rsDD.Close(); LessonCon.Close(); }
<%@ WebHandler Language="C#" Class="Update" %> using System; using System.Web; using System.Data; using System.Configuration; using System.Data.SqlClient; public void ProcessRequest (HttpContext context) { string getID = context.Request["DropDownList1"]; string getMyID = context.Request["MyID"]; //context.Response.Write(getID + "-" + getMyID); var LessonCon = new SqlConnection(); SqlCommand LessonCMD = new SqlCommand(); LessonCon = new SqlConnection(ConfigurationManager.ConnectionStrings["Virtual-Learning"].ConnectionString); LessonCon.Open(); SqlCommand getDD = new SqlCommand("SELECT DropDown.ddID, Dropdown2.MyID FROM DropDown INNER JOIN Dropdown2 ON DropDown.ddID = Dropdown2.ddid WHERE DropDown.ddID = @ddID AND Dropdown2.MyID = @MyID", LessonCon); getDD.Parameters.Add(new SqlParameter("@ddID", getID)); getDD.Parameters.Add(new SqlParameter("@MyID", getMyID)); SqlDataReader rsDD; rsDD = getDD.ExecuteReader(); if (rsDD.Read()) { context.Response.Write("The record already exist,
please choose another one."); } else try { SqlCommand objCmd = new SqlCommand(); string strSQL; strSQL = "insert into Dropdown2 (ddid, MyID)values(@ddid, @MyID)"; objCmd = new SqlCommand(strSQL, LessonCon); objCmd.Parameters.Add(new SqlParameter("@ddid", getID)); objCmd.Parameters.Add(new SqlParameter("@MyID", getMyID)); objCmd.ExecuteNonQuery(); context.Response.Write("Recorded inserted Successfully!"); } catch (Exception ex) { context.Response.Write("Sorry, there seems to be an issue,
please try again later."); } finally { rsDD.Close(); LessonCon.Close(); } }
Preview
Tags
live JQuery view of the data
live JQuery view of the data
live JQuery view of the data
ASP.NET C# code loads records from a database into a select menu
ASP.NET (C# Version) SQL Server Database Driven Dropdown Menu