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.

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

Any help is appreciated!

5
Contributors
5
Replies
15
Views
9 Years
Discussion Span
Last Post by Ezzaral
-1

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

ddlflightnum.DataValueField = "flightnum"

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.

0

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

0

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

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.