Binding a column to a dropdownlist at runtim

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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 vb.net with sql server 2000. Its for a
web development

i tried this but it never worked
With Dropdownlist1

.DataSource = CreateDataSet(strSQL2)
.DataBind()

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
scmd.Parameters.Add(sqlParam)
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
sda.Fill(ds)

Return ds
End Function

Thank you
 
I think that you have to assign a DataMember to your DropDownList
 
Howdy,

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
'Daniel%'

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

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.
 
Back
Top