Populate DropDown List from Sql Database

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2008
Posts: 13
Reputation: firebirds98 is an unknown quantity at this point 
Solved Threads: 0
firebirds98 firebirds98 is offline Offline
Newbie Poster

Populate DropDown List from Sql Database

 
0
  #1
Feb 19th, 2008
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.


  1. sql = "Select flightnum from Air_flight"
  2. ddlflightnum.DataSource = objdb.getDataSet(sql)
  3. ddlflightnum.DataBind()


Any help is appreciated!
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 13
Reputation: firebirds98 is an unknown quantity at this point 
Solved Threads: 0
firebirds98 firebirds98 is offline Offline
Newbie Poster

Re: Populate DropDown List from Sql Database

 
0
  #2
Feb 19th, 2008
Please Delete this thread I figured it out I needed the line of code

ddlflightnum.DataValueField = "flightnum"
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,080
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Solved Threads: 68
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: Populate DropDown List from Sql Database

 
0
  #3
Feb 19th, 2008
Yup, you cannot populate a DropDownList like that. Code for that is as follows:
  1. Dim conn As New SqlConnection( connection )
  2. Dim cmdSelect As New SqlCommand( ""Select flightnum from Air_flight", conn )
  3. Dim dtrReader As SqlDataReader
  4.  
  5. Try
  6. conn.Open()
  7.  
  8. dtrReader = cmdSelect.ExecuteReader()
  9.  
  10. if dtrReader.HasRows then
  11. ddlflightnum.DataSource = dtrReader
  12. ddlflightnum.DataValueField = "flightnum"
  13. ddlflightnum.DataTextField = "flightnum"
  14. ddlflightnum.DataBind()
  15. end if
  16.  
  17. dtrReader.Close()
  18. conn.Close()
  19. Catch
  20. End Try
  21.  
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 30
Reputation: dilipv is an unknown quantity at this point 
Solved Threads: 4
dilipv dilipv is offline Offline
Light Poster

Re: Populate DropDown List from Sql Database

 
0
  #4
Feb 20th, 2008
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
Dilip Kumar Vishwakarma
Programmer
.Net Consulting
If this post is answer for your query then don't forget to mark as an answer.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC