Binding a column to a dropdownlist at runtim



Hello everyone

can you please help me in finding out how i can bind a column to a
dropdownlist at run time. I'm using with sql server 2000. Its for a
web development

i tried this but it never worked
With Dropdownlist1

.DataSource = CreateDataSet(strSQL2)

end with

Private Function CreateDataSet(ByVal strSQL As String, _
Optional ByVal sqlParam As SqlParameter = Nothing) As DataSet

Dim scnnNW As New SqlConnection(strConn)

' A SqlCommand object is used to execute the SQL commands.
Dim scmd As New SqlCommand(strSQL, scnnNW)

If Not IsNothing(sqlParam) Then
End If

' A SqlDataAdapter uses the SqlCommand object to fill a DataSet.
Dim sda As New SqlDataAdapter(scmd)

' Create and Fill a new DataSet.
Dim ds As New DataSet

Return ds
End Function

Thank you



Ignacio Machin \( .NET/ C# MVP \)

I think that you have to assign a DataMember to your DropDownList





DropDownList exposes two useful properties:
DataTextField (column mapped to item text )
DataValueField (column mapped to item value)
Simple example. Your query returns several columns, for instance:

SELECT UserId, UserName, UserDateOfBirth FROM UserTable WHERE Login LIKE

Now, you need to bind result set to a dropdownlist, using user name as text
and user id as value.

With Dropdownlist1

.DataTextField = "UserName"
.DataValueField = "UserId"
.DataSource = CreateDataSet(strSQL2)

end with

Please note, if dataset contains more than one table, set DataMember
property to specify the name of the table drop down list should be bound to

Hope this helps.

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question