将下拉列表绑定到SQLDataSource

时间:2014-11-03 17:27:36

标签: asp.net vb.net

我正在努力将下拉列表绑定到数据源。请参阅以下代码:

Private _ConString As String

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        _ConString = ConfigurationManager.ConnectionStrings("GeniedbConnection").ConnectionString
        SqlDataSourceNicheDuplicates.ConnectionString = _ConString
        SqlDataSourceCreatedDate.ConnectionString = _ConString
        SqlDataSourceCreatedDate.SelectCommand = "SELECT dateadded distinct convert(varchar,dateadded,103) as dateadded dbNicheDuplicates"
        DDLCreatedDate.DataTextField = "dateadded"
        DDLCreatedDate.DataValueField = "dateadded"
        DDLCreatedDate.DataBind()
end sub

如果我在SQL Studio Manager中运行SQL语句,则会返回许多结果。我做错了什么?

我花了一些时间谷歌搜索这个。例如,我查看了这个问题:Populating an ASP.Net DropDownList using VB.Net coding in code-behind file

2 个答案:

答案 0 :(得分:0)

您没有设置DataSource

DDLCreatedDate.DataSource = <results>
DDLCreatedDate.DataBind()

您没有列出您使用的对象类型,因此我不确定遗漏的陈述究竟是什么。

答案 1 :(得分:0)

只需向lincolnk's答案添加更多信息即可。你需要这样做 - DDLCreatedDate.DataSource = SqlDataSourceCreatedDate;

SqlDataSourceCreatedDate.SelectCommand = "SELECT dateadded distinct convert(varchar,dateadded,103) as dateadded dbNicheDuplicates"
DDLCreatedDate.DataSource = SqlDataSourceCreatedDate;
DDLCreatedDate.DataTextField = "dateadded"
DDLCreatedDate.DataValueField = "dateadded"
DDLCreatedDate.DataBind()