| | |
Populate DropDown List from Sql Database
Please support our ASP.NET advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2008
Posts: 13
Reputation:
Solved Threads: 0
I am trying to populate a drop downlist on my page with data in flightnum from the table Air_Flight, my dropdownlist keeps having System.Data.Datarow in the dropdownlist. Im pretty sure im one line of code away from getting the actual values in the dropdownlist, I am just not sure which line of code it is. Also the dropdownlist sees how many records are in the db. For instance if i have the flight numbers '123' , '987', '487' it will have 3 System.Data.Datarow line in the dropdownlist and if I had another flight_num it will then have 4 System.Data.Datarow in the dropdownlist.
Any help is appreciated!
ASP.NET Syntax (Toggle Plain Text)
sql = "Select flightnum from Air_flight" ddlflightnum.DataSource = objdb.getDataSet(sql) ddlflightnum.DataBind()
Any help is appreciated!
•
•
Join Date: Sep 2007
Posts: 1,080
Reputation:
Solved Threads: 68
Yup, you cannot populate a DropDownList like that. Code for that is as follows:
Now if you do not specify DataTextField, it will default to the DataValueField.
You can populate it through a dataset how you were as well, but I would try to steer you away from datasets as it takes up a lot of resources and the above code is much more efficient.
ASP.NET Syntax (Toggle Plain Text)
Dim conn As New SqlConnection( connection ) Dim cmdSelect As New SqlCommand( ""Select flightnum from Air_flight", conn ) Dim dtrReader As SqlDataReader Try conn.Open() dtrReader = cmdSelect.ExecuteReader() if dtrReader.HasRows then ddlflightnum.DataSource = dtrReader ddlflightnum.DataValueField = "flightnum" ddlflightnum.DataTextField = "flightnum" ddlflightnum.DataBind() end if dtrReader.Close() conn.Close() Catch End Try
You can populate it through a dataset how you were as well, but I would try to steer you away from datasets as it takes up a lot of resources and the above code is much more efficient.
•
•
Join Date: Feb 2008
Posts: 30
Reputation:
Solved Threads: 4
hi firebirds98,
You can populate DropDownlList from Sql database as follows.
connection_object.start_connection()
datareader_object = connection_object.executequery("SELECT city_name from City")
DropDownList1.Items.Clear()
While dr.Read
DropDownList1.Items.Add(dr(0))
End While
dr.Close()
connection_object.close_connection()
Hope this will help you.
Thanks & Regards
Dilipv
You can populate DropDownlList from Sql database as follows.
connection_object.start_connection()
datareader_object = connection_object.executequery("SELECT city_name from City")
DropDownList1.Items.Clear()
While dr.Read
DropDownList1.Items.Add(dr(0))
End While
dr.Close()
connection_object.close_connection()
Hope this will help you.
Thanks & Regards
Dilipv
Dilip Kumar Vishwakarma
Programmer
.Net Consulting
If this post is answer for your query then don't forget to mark as an answer.
Programmer
.Net Consulting
If this post is answer for your query then don't forget to mark as an answer.
![]() |
Similar Threads
- Using Code 5 System to create forms...I got 3 problems (PHP)
- Using Code 5 System to create forms...I got 3 problems (ASP.NET)
- Using Code 5 System to create forms...I got 3 problems (JavaScript / DHTML / AJAX)
- Populating & Retrieving Data in a listbox : ASP.NET (w/ VB.NET) (ASP.NET)
Other Threads in the ASP.NET Forum
- Previous Thread: [B]DropDownList, TextBox to GridView.....[/B]
- Next Thread: Title : Creating setup of web application Product
| Thread Tools | Search this Thread |
.net 2.0 3.5 ajax alltypeofvideos appliances asp asp.net beginner box browser businesslogiclayer button c# cac checkbox class commonfunctions compatible content contenttype control countryselector courier dataaccesslayer database datagrid datagridview datalist deployment development dgv dialog dropdownlist dropdownmenu dynamic dynamically edit embeddingactivexcontrol fileuploader fill findcontrol flash flv gridview gudi iis javascript list listbox login menu microsoft mouse mssql nameisnotdeclared news novell numerical opera order panelmasterpagebuttoncontrols problem radio ratings redirect registration relationaldatabases reportemail schoolproject search security serializesmo.table sessionvariables silverlight smoobjects software sql sql-server sqlserver2005 ssl tracking treeview validatedate validation vb.net videos vista visual-studio visualstudio vs2008 web webapplications webarchitecture webdevelopment webprogramming webservice wizard xsl youareanotmemberofthedebuggerusers






