Search code examples
asp.netdrop-down-menubinddatatextfield

Cannot Bind Value to Dropdownlist


Why can't I binding value to the dropdownlist? It always return message: DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'Name'.

I have a dropdownlist:

<asp:DropDownList ID="Parameter_Dropdownlist" runat="server">
</asp:DropDownList>   

Then on code-behind:

If myCEConnection.State = ConnectionState.Closed Then
    Try
        myCEConnection.Open()
    Catch
        Return
    End Try
End If

Dim reader As SqlCeDataReader
Dim myCommand As SqlCeCommand = myCEConnection.CreateCommand()

myCommand.CommandText = "SELECT Name, Code FROM Room"
reader = myCommand.ExecuteReader()

DT.Load(reader)

DDL.DataSource = DT
DDL.DataTextField = DT.Columns("Name").ColumnName.ToString()
DDL.DataValueField = DT.Columns("Code").ColumnName.ToString()
DDL.DataBind()

myCEConnection.Close()

How can I bind the value to the dropdownlist? Please help. Thank you very much.


Solution

  • Try this, adding example :-

    If myCEConnection.State = ConnectionState.Closed Then
       Try
          myCEConnection.Open()
      Catch
          Return
      End Try
    End If
    
    Dim reader As SqlCeDataReader
    Dim myCommand As SqlCeCommand = myCEConnection.CreateCommand()
    
    myCommand.CommandText = "SELECT Name, Code FROM Room"
    reader = myCommand.ExecuteReader()
    
    DT.Load(reader)
    
    DDL.DataSource = DT
    DDL.DataTextField = DT.Columns(0).ColumnName.ToString()
    DDL.DataValueField = DT.Columns(1).ColumnName.ToString()
    DDL.DataBind()
    
    myCEConnection.Close()