Binding a column to a dropdownlist at runtim

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

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

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

Guest

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.
 

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

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top