954,595 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Populate DropDown List from Sql Database

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.

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

Any help is appreciated!

firebirds98
Newbie Poster
15 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
 

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

ddlflightnum.DataValueField = "flightnum"

firebirds98
Newbie Poster
15 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
 

Yup, you cannot populate a DropDownList like that. Code for that is as follows:

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

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.

SheSaidImaPregy
Veteran Poster
1,080 posts since Sep 2007
Reputation Points: 43
Solved Threads: 68
 

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

dilipv
Light Poster
30 posts since Feb 2008
Reputation Points: 10
Solved Threads: 4
 

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

Netcode
Veteran Poster
1,021 posts since Jun 2009
Reputation Points: 43
Solved Threads: 67
 

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.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You