943,913 Members | Top Members by Rank

Ad:
  • ASP.NET Discussion Thread
  • Unsolved
  • Views: 24901
  • ASP.NET RSS
Feb 19th, 2008
0

Populate DropDown List from Sql Database

Expand Post »
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.


ASP.NET Syntax (Toggle Plain Text)
  1. sql = "Select flightnum from Air_flight"
  2. ddlflightnum.DataSource = objdb.getDataSet(sql)
  3. ddlflightnum.DataBind()


Any help is appreciated!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
firebirds98 is offline Offline
15 posts
since Feb 2008
Feb 19th, 2008
-1

Re: Populate DropDown List from Sql Database

Please Delete this thread I figured it out I needed the line of code

ddlflightnum.DataValueField = "flightnum"
Reputation Points: 10
Solved Threads: 0
Newbie Poster
firebirds98 is offline Offline
15 posts
since Feb 2008
Feb 19th, 2008
0

Re: Populate DropDown List from Sql Database

Yup, you cannot populate a DropDownList like that. Code for that is as follows:
ASP.NET Syntax (Toggle Plain Text)
  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.
Reputation Points: 43
Solved Threads: 68
Veteran Poster
SheSaidImaPregy is offline Offline
1,080 posts
since Sep 2007
Feb 20th, 2008
0

Re: Populate DropDown List from Sql Database

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
Reputation Points: 10
Solved Threads: 4
Light Poster
dilipv is offline Offline
30 posts
since Feb 2008
Aug 5th, 2011
0
Re: Populate DropDown List from Sql Database
Please Delete this thread I figured it out I needed the line of code

ddlflightnum.DataValueField = "flightnum"
Delete? you should mark it as solved since you've got the right thing
Reputation Points: 43
Solved Threads: 67
Veteran Poster
Netcode is offline Offline
1,014 posts
since Jun 2009
Aug 5th, 2011
0
Re: Populate DropDown List from Sql Database
This thread was from 2008 and was only resurrected by someone promoting their blog. That post has been deleted and I am closing this thread to prevent any further confusion.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in ASP.NET Forum Timeline: insert,update,delete & select
Next Thread in ASP.NET Forum Timeline: how to handle the database when multiusers are inserting data at same time





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC