| | |
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: How do you search all current sessions on the sever, or within the website?
Views: 8826 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for ASP.NET
.net 2.0 3.5 activexcontrol advice ajax asp asp.net bc30451 bottomasp.net browser businesslogiclayer button c# c#gridviewcolumn checkbox child class click compatible confirmationcodegeneration content contenttype countryselector courier css database datagrid datagridview datagridviewcheckbox datalist deadlock development dgv dropdown dropdownmenu edit feedback flash flv folder form forms google grid gridview homeedition hosting identity iframe iis index javascript jquery languages list maps menu mono mssql multistepregistration nameisnotdeclared object objects order problem profile ratings refer rotatepage save search security serializesmo.table session silverlight smartcard sql suse textbox tracking typeof unauthorized update validation vb vb.net video view virtualdirectory vista visual-studio visualstudio vs2008 web webarchitecture webdevelopemnt webdevelopment wizard xml






