Tuesday, July 15, 2014
One of the most commonly used server controls in ASP.NET is the DropDownList control. In this blog I will show you how to bind a DataTable to a DropDownList control.
1. Create a .aspx page
2. Click on the "Toolbox" panel
3. Drag the DropDownList control into a design surface
9. If you look at the HTML source code you will see the following
1. Create a .aspx page
2. Click on the "Toolbox" panel
4. Create a connection string in the Web.Config file
<connectionStrings> <add name="NorthwindConnectionString" connectionString="Data Source=(local); Initial Catalog=Northwind;Integrated Security=True" providerName="System.Data.SqlClient"/> </connectionStrings>5. In the code behind page(.cs) page of the .aspx page, type in the following lines at the top to use the following libraries
using System.Web.Configuration; using System.Data.SqlClient; using System.Data;6. Create a new method call BindCategoriesList() that returns void
protected void BindCategoriesList() { DataTable dtCategories = new DataTable(); string connectString = WebConfigurationManager.ConnectionStrings["NorthwindConnectionString"]. ConnectionString; using (SqlConnection conn = new SqlConnection(connectString)) { SqlCommand cmd = new SqlCommand("SELECT CategoryID,CategoryName FROM Categories", conn); conn.Open(); SqlDataAdapter adapter = new SqlDataAdapter(cmd); adapter.Fill(dtCategories); DropDownList1.DataSource = dtCategories; DropDownList1.DataTextField = dtCategories.Columns["CategoryName"].ToString(); DropDownList1.DataValueField = dtCategories.Columns["CategoryID"].ToString(); DropDownList1.DataBind(); } }The code above creates new DataTable call dtCategories, then it connects to the database using the "NorthwindConnectionString", and queries the CategoryID, and CategoryName in the Categories table in the Northwind database. The query results are then stored in the DataTable. The next step is to use the data in the dtCategories DataTable to populate the DropDownList1 control on our .aspx page. The following lines are used to bind the dtCategories to the DropDowndList1 control. Read the code comment to get the explanation
//used to set the DropDownList1.DataSource to dtCategories DropDownList1.DataSource = dtCategories; //set the text field of the dropdownlist to the "CategoryName" value in the database DropDownList1.DataTextField = dtCategories.Columns["CategoryName"].ToString(); //set the value field of the dropdownlist to the "CategoryID" value in the database DropDownList1.DataValueField = dtCategories.Columns["CategoryID"].ToString();7. Call the BindCategoriesList() method in the Page_Load method
protected void Page_Load(object sender, EventArgs e) { BindCategoriesList(); }8. Run the application and you will see the DropDownList1 control populated with the Category Names in the Northwind database
9. If you look at the HTML source code you will see the following
<select name="DropDownList1" id="DropDownList1"> <option value="1">Beverages</option> <option value="2">Condiments</option> <option value="3">Confections</option> <option value="4">Dairy Products</option> <option value="5">Grains/Cereals</option> <option value="6">Meat/Poultry</option> <option value="7">Produce</option> <option value="8">Seafood</option> </select>
Related Blogs:
- Bind a DataTable To A DropDownList
- (DropDownList) Setting Default Value on First Page Load
- Use The DropDownList Control To Populate GridView
- Bind DropDownList To A List Of Objects
Subscribe to:
Post Comments (Atom)
Search This Blog
Tags
Web Development
Linux
Javascript
DATA
CentOS
ASPNET
SQL Server
Cloud Computing
ASP.NET Core
ASP.NET MVC
SQL
Virtualization
AWS
Database
ADO.NET
AngularJS
C#
CSS
EC2
Iaas
System Administrator
Azure
Computer Programming
JQuery
Coding
ASP.NET MVC 5
Entity Framework Core
Web Design
Infrastructure
Networking
Visual Studio
Errors
T-SQL
Ubuntu
Stored Procedures
ACME Bank
Bootstrap
Computer Networking
Entity Framework
Load Balancer
MongoDB
NoSQL
Node.js
Oracle
VirtualBox
Container
Docker
Fedora
Java
Source Control
git
ExpressJS
MySQL
NuGet
Blogger
Blogging
Bower.js
Data Science
JSON
JavaEE
Web Api
DBMS
DevOps
HTML5
MVC
SPA
Storage
github
AJAX
Big Data
Design Pattern
Eclipse IDE
Elastic IP
GIMP
Graphics Design
Heroku
Linux Mint
Postman
R
SSL
Security
Visual Studio Code
ASP.NET MVC 4
CLI
Linux Commands
Powershell
Python
Server
Software Development
Subnets
Telerik
VPC
Windows Server 2016
angular-seed
font-awesome
log4net
servlets
tomcat
AWS CloudWatch
Active Directory
Angular
Blockchain
Collections
Compatibility
Cryptocurrency
DIgital Life
DNS
Downloads
Google Blogger
Google Chrome
Google Fonts
Hadoop
IAM
KnockoutJS
LINQ
Linux Performance
Logging
Mobile-First
Open Source
Prototype
R Programming
Responsive
Route 53
S3
SELinux
Software
Unix
View
Web Forms
WildFly
XML
cshtml
githu
Aivivu chuyên vé máy bay, tham khảo
ReplyDeletevé máy bay đi Mỹ giá rẻ
vé máy bay từ atlanta về việt nam
bao giờ có chuyến bay từ đức về việt nam
dịch vụ vé máy bay tại nga
mua vé máy bay từ anh về việt nam
mua vé máy bay giá rẻ từ pháp về việt nam
chi phi ve may bay cho chuyen gia nuoc ngoai