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!

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

ddlflightnum.DataValueField = "flightnum"

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.

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

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.