Asp.Net Dropdown Control items from Database

Assalamualaykum Wr Br…Today we explore dropdown control list items binding from database table on to UI. Before we quick start, there are few properties that we need to remember when dealing with Dropdown controls. They are:
1. DataTextField
2. DataValueField
3. DataSource
4. DataBind
There are many other properties like selectedIndex, IndexChanged but we focus on above.
Lets start the POC:
DDl
I just drag & drop the Dropdownlist control from toolbox on form beside Qualification label. The items of this dropdownlist should be populated from Table Register and column Qualification. To accomplish it, I write Sql Query as: select qualification from register in sqlcommand class or sqldataadapter as shown below:
ddl1
As I need dropdownlist to be populated on page_load hence I am calling method filldropdown
In filldropdown method,sqlconnection is established then sqldataadapter executes the query fetches data and fills in dataset which becomes datasource to dropdownlist and finally binds to it. The above code produces following output:
ddl5
One thing to be noted is that all items are being populated from database, if we comment the statement Dropdownlist1.AppendDataBoundItems=true.
If we uncomment it then it is possible to add items manually to dropdownlist which is done by statement as dropdownlist1.items.add(“”); whose output is shown as:
ddl4
Jazakallah khair…thats all for the day…

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s