473,466 Members | 1,554 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 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

Mar 2 '07 #1
2 5984
I think that you have to assign a DataMember to your DropDownList
"Daniel" <Da****@discussions.microsoft.comwrote in message
news:80**********************************@microsof t.com...
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

Mar 2 '07 #2
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.
--
Milosz
"Daniel" wrote:
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
Mar 5 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: ShadowsOfTheBeast | last post by:
hi all i am trying to bind data to my datagrid from a listbox which i think it should work but an error is coming up saying i have to bind to a datasource that implements the Inumerable or...
1
by: cw bebop | last post by:
Is there a way to bind two columns from a table to one DropDownList? Below I have the column databound to DropDownList1 and would like to also have the column bound to the same DropDownList1...
1
by: Harold | last post by:
Hi all, having a little difficulty binding up a dropdownlist control in a datagrid column. I know I must be missing something Here are 2 methods. One is the DataGrid's ItemDataBound event to catch...
1
by: Mike Bridge | last post by:
I'm getting a nonsensical error when binding a DataTable with one row to a DropDownList. The DataTable's row contains the fields "id" (INT), "description" (VARCHAR) and with a couple other...
0
by: ba | last post by:
I'm using asp.net 2.0 and am writing a webform using the formview control. The <%# Bind("") %> syntax makes sense for textboxes, but I cannot figure out how to bind a dropdown list to a column....
9
by: Timm | last post by:
I have an ASP.NET 2.0 page with two DropDownLists. I am using declarative data binding wherever possible and trying to minimize the use of code. The list of values in DropDownList DDL2 should be...
3
by: mateo | last post by:
Hello, I have a GridView inside which i want to show 3 dropdownList (one for each Column). So i've created 3 TemplateField Continent Country City
6
by: p.mc | last post by:
Hi all, I'm having major problems with a userControl which contains a datagrid. My problem concerns data binding. The Page_Load() procedure calls the DataBind procedure to bind the datagrid...
1
by: suganya | last post by:
Hi I have "Specialty" table with 2 fields (SPID,Specialty). To bind data from "Specialty: table to the dropdownlist during page load, I have given the following coding protected void...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.