无法将值绑定到下拉列表

时间:2012-05-04 06:52:41

标签: asp.net drop-down-menu bind datatextfield

为什么我不能将值绑定到下拉列表?它始终返回消息:DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'Name'.

我有一个下拉列表:

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

然后是代码隐藏:

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()

如何将值绑定到下拉列表?请帮忙。非常感谢你。

1 个答案:

答案 0 :(得分:1)

试试这个,添加例子: -

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()