0

I am struggling to bind a Drop Down List to a Data Source. Please see the code below:

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

If I run the SQL statement in SQL Studio Manager then many results are returned. What am I doing wrong?

I have spent some time Googling this. For example I looked at this question: Populating an ASP.Net DropDownList using VB.Net coding in code-behind file

Community
  • 1
  • 1
w0051977
  • 15,099
  • 32
  • 152
  • 329

2 Answers2

0

You are not setting DataSource.

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

You don't list what type of objects your working with so I'm not sure exactly what the missing statement is.

lincolnk
  • 11,218
  • 4
  • 40
  • 61
0

Just adding more info to lincolnk's answer. You need to do this - 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()
Community
  • 1
  • 1
Arindam Nayak
  • 7,346
  • 4
  • 32
  • 48
  • Thanks, but the drop down list is still empty. – w0051977 Nov 03 '14 at 18:01
  • @w0051977, are you sure this SQL `SELECT dateadded distinct convert(varchar,dateadded,103) as dateadded dbNicheDuplicates` is working in management console? – Arindam Nayak Nov 03 '14 at 18:03
  • 1
    Sorry, my fault. There were two tables: dbNicheDuplicates and dbo.dbNicheDuplicate. dbo.dbNicheDuplicate had no data. – w0051977 Nov 03 '14 at 18:12