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
 
Ad

Advertisements

I

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

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

Advertisements

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

Similar Threads

DropDownList 1
dropdownlist 4
Help!! Can someone please check my code? 8
Webservice error - please help 2
SqlCommandBuilder 4
ReportViewer 2010 SubReport 0
Consume Webservice question 2
DropDownList DataGrid 1

Top