Binding a dropdown listbox

Im trying to bind a dropdown listbox with a table in SQL Server 7.0. Whenerver i use this code , i get value: "System.Data.Common.dbDataRecord".
Following is my code :

Sub  Page_Load()
Dim strSqlCities as String = "Select city from city"
  Dim strConnection as String = "server=Maxood;database=watal;user id=sa;password=;"
 
  
  Dim objConn as New SqlConnection(strConnection)


  Dim Cmd as New SqlCommand(strSqlCities,objConn)

  objConn.Open()
  cbocity.DataSource =Cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
  cbocity.DataBind() 
 
  objConn.Close()
End Sub


Could someone tell me what is wrong




Answer this question

Binding a dropdown listbox

  • PaulJackson1999

    Now the complier gives error:
    Compiler Error Message: BC30456: 'DisplayValue' is not a member of 'System.Web.UI.WebControls.DropDownList'.

    Where are we wrong exctly Would you care to look in to it please, thanx

  • simon_wang

    I assume that web questions are asked on an ASP.NET forum.  My advice was for a winforms app.

    In ASP.NET they are called DataValueField and DataTextField.  The latter is the one that is displayed.


  • paVF

    Could you knidly give me some code example.Please have a look on my code and do let me know what is really missing there.
    I appreciate your response...thankyou 

  • Rod Colledge

    It has worked ...yes!!!Both "DataValueField" and "DataTextField" are working.Thanks a lot Christian for your precious help and prompt replies.

    Wonder if you can tell me why there are 2 properties("DataValueField" and "DataTextField") and which one is better to use in which scenario.

    Again thanks a lot for your great help. 

  • AndersChen

    objConn.Open()
      cbocity.DataSource =Cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
      cboCity.DisplayValue = "TheColumnName"
      cbocity.DataBind() 

  • Nagendran Sellaiah

    When you set a datasource to a listbox, and the source is not an array of strings, you need to set the DisplayMember property, so the control knows where to look to get these values from the data source.  Set it to be the column name you want, as it stands, it's using ToString to get a string out of the object it finds when it iterates through the collection.


  • Binding a dropdown listbox