473,725 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Binding data to DropDownList...

RAB
I have the following code:

<%@ Page Language="VB" %>
<script runat="server">

Function GetName() As System.Data.IDa taReader
Dim connectionStrin g As String = 'path to datasource
Dim dbConnection As System.Data.IDb Connection = New
System.Data.Ole Db.OleDbConnect ion
(connectionStri ng)

Dim queryString As String = "SELECT [Customer].[ID],
[Customer].[LName], [Customer].[FName] FROM
[Customer]"
Dim dbCommand As System.Data.IDb Command = New
System.Data.Ole Db.OleDbCommand
dbCommand.Comma ndText = queryString
dbCommand.Conne ction = dbConnection

dbConnection.Op en
Dim dataReader As System.Data.IDa taReader =
dbCommand.Execu teReader

(System.Data.Co mmandBehavior.C loseConnection)

Return dataReader
End Function

Sub Page_Load(sende r As Object, e As EventArgs)
Listbox1.dataso urce = GetName()
Listbox1.databi nd()
End Sub

</script>
<html>
<head>
</head>
<body>
<form runat="server">
<p>
</p>
<p>
<asp:ListBox id="ListBox1" runat="server"
DataTextField=" LName" DataValueField= "ID"></asp:ListBox>
</p>

<p>
<!-- Insert content here -->
</p>
</form>
</body>
</html>
If I wanted to populate my dropdownListbox with first and last name,
how would I code this?

Thanks,
RABMissouri2007

Mar 12 '07 #1
5 1212
On 12 Mar, 16:00, "RAB" <rabmisso...@ya hoo.comwrote:
I have the following code:

<%@ Page Language="VB" %>
<script runat="server">

Function GetName() As System.Data.IDa taReader
Dim connectionStrin g As String = 'path to datasource
Dim dbConnection As System.Data.IDb Connection = New
System.Data.Ole Db.OleDbConnect ion
(connectionStri ng)

Dim queryString As String = "SELECT [Customer].[ID],
[Customer].[LName], [Customer].[FName] FROM
[Customer]"
Dim dbCommand As System.Data.IDb Command = New
System.Data.Ole Db.OleDbCommand
dbCommand.Comma ndText = queryString
dbCommand.Conne ction = dbConnection

dbConnection.Op en
Dim dataReader As System.Data.IDa taReader =
dbCommand.Execu teReader

(System.Data.Co mmandBehavior.C loseConnection)

Return dataReader
End Function

Sub Page_Load(sende r As Object, e As EventArgs)
Listbox1.dataso urce = GetName()
Listbox1.databi nd()
End Sub

</script>
<html>
<head>
</head>
<body>
<form runat="server">
<p>
</p>
<p>
<asp:ListBox id="ListBox1" runat="server"
DataTextField=" LName" DataValueField= "ID"></asp:ListBox>
</p>

<p>
<!-- Insert content here -->
</p>
</form>
</body>
</html>

If I wanted to populate my dropdownListbox with first and last name,
how would I code this?

Thanks,
RABMissouri2007
It is always a good idead to specifically name the fields in your data
source to which you're binding, so that the Value and Text are made
explicit for the DataValueField and DataTextField properties.

Listbox1.DataSo urce = GetName();
Listbox1.DataVa lueField = "myValueNam e";
Listbox1.DataTe xtField = "myTextName ";
Listbox1.DataBi nd();

Mar 12 '07 #2
Easiest way to to change the select to output the name in the format you
wish to display. Something like "SELECT [Customer].[ID],
[Customer].[LName] + ', ' + [Customer].[FName] FROM
[Customer]"
Lloyd Sheen
"RAB" <ra*********@ya hoo.comwrote in message
news:11******** **************@ q40g2000cwq.goo glegroups.com.. .
>I have the following code:

<%@ Page Language="VB" %>
<script runat="server">

Function GetName() As System.Data.IDa taReader
Dim connectionStrin g As String = 'path to datasource
Dim dbConnection As System.Data.IDb Connection = New
System.Data.Ole Db.OleDbConnect ion
(connectionStri ng)

Dim queryString As String = "SELECT [Customer].[ID],
[Customer].[LName], [Customer].[FName] FROM
[Customer]"
Dim dbCommand As System.Data.IDb Command = New
System.Data.Ole Db.OleDbCommand
dbCommand.Comma ndText = queryString
dbCommand.Conne ction = dbConnection

dbConnection.Op en
Dim dataReader As System.Data.IDa taReader =
dbCommand.Execu teReader

(System.Data.Co mmandBehavior.C loseConnection)

Return dataReader
End Function

Sub Page_Load(sende r As Object, e As EventArgs)
Listbox1.dataso urce = GetName()
Listbox1.databi nd()
End Sub

</script>
<html>
<head>
</head>
<body>
<form runat="server">
<p>
</p>
<p>
<asp:ListBox id="ListBox1" runat="server"
DataTextField=" LName" DataValueField= "ID"></asp:ListBox>
</p>

<p>
<!-- Insert content here -->
</p>
</form>
</body>
</html>
If I wanted to populate my dropdownListbox with first and last name,
how would I code this?

Thanks,
RABMissouri2007
Mar 12 '07 #3
RAB
What would one set Listbox1 DataValueField equal to, if one used the
ammended select statement?

Thanks,
RABMissouri2007

Mar 12 '07 #4
RAB
I figured it out. If one alters the select statement to the
following:

"SELECT [Customer].[ID],
[Customer].[LName] + ', ' + [Customer].[FName] as FullName FROM
[Customer]"
Then use "FullName" for the DataValueField

RABMissouri2007

Mar 12 '07 #5
Each item in the listbox has both a display and value. When you select the
item you will use the value rather than the display value.

To do this you will have to tell the binding which column has the display
value and which has the value value.

Use the DataTextField and DataValueField to set those. You will need to
change your select statement to then return both the display and actual
values to the datareader.

Lloyd Sheen

"RAB" <ra*********@ya hoo.comwrote in message
news:11******** **************@ s48g2000cws.goo glegroups.com.. .
What would one set Listbox1 DataValueField equal to, if one used the
ammended select statement?

Thanks,
RABMissouri2007
Mar 12 '07 #6

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

Similar topics

10
3093
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 icollection...but i thought list boxes did implement one of the above as they do have their items collection property, can anyone help?
2
2236
by: rmorvay | last post by:
I am trying to dynamically build a dropdownlist and bind it to a cell in a grid. I tried to utilize the following code but I am stuck at the point where I bind the dropdownlist to the grid cell. I get the following message for this code but I suspect that the " UltraWebGrid1.Rows.Cells.Value = ddlGeography;" is not how you bind the dropdownlist control to the grid. ERROR: "The type 'System.Web.UI.WebControls.DropDownList' must be...
0
1160
by: Goncalo Faria | last post by:
Hi. I'll go directly to my problem: I have a repeater that is binded with data. For each data row I want the user to be able to change a field, that is represented by a DropDownList. My problem is that when binding the DropDownList I am not
3
2516
by: Binod Nair | last post by:
Hi All, This is what I am trying to do.I have an aspx page with the the following code block. <asp:DropDownList id="DropDownList1" runat="server" DataSource="<%# GetUsers%>" DataTextField="user_email" DataValueField="user_id"> </asp:DropDownList> GetUsers is defined in the .vb file
5
18540
by: sck10 | last post by:
Hello, I have a GridView that is using the following to connect to a SQL Server 2000 stored procedure: <asp:SqlDataSource ID="dsWebDocList" SelectCommand="sp_web_WebDocument" ConnectionString="<%$ ConnectionStrings:cnnSQL_Connect %>" Runat="server"> <SelectParameters> <asp:Parameter Type="String" DefaultValue="FindWebDocAll"
5
4229
by: Vigneshwar Pilli via DotNetMonster.com | last post by:
string connectionString1 = "server=(local); user=sa;password=sa; database=sonic"; System.Data.SqlClient.SqlConnection dbConnection1 = new System.Data.SqlClient.SqlConnection(connectionString1); System.Data.SqlClient.SqlCommand dbCommand1 = new System.Data.SqlClient.SqlCommand();
9
5336
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 (filtered) dependent upon the selection in DDL1. I think this inevitably needs some code, but I'd be happy to be told otherwise! I have some code to handle OnSelectedIndexChanged for DDL1 that sets the FilterExpression associated with the...
6
5743
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 to a DataSet. If i include an if statement to prevent the data binding from occuring on a page PostBack in the following way:
2
2609
by: bmayer | last post by:
I am using a detail view and binding it to a sql data source (at some point in the future it will be an object data source). I have seen the article "Working with Data is ASP.NET 2.0 :: Customizing the Data Modification Interface", and its description of how to turn one of the fields into a template field, then add a drop down list. I want to do the same thing except I want two drop down lists, with the second one populating its values...
2
1873
by: Igor | last post by:
Hi I build a page that has a dropdownlist control bound with data from a database. When user select an item from the dropdownlist I want a label to be filled with a result from a stored procedure. The stored procedure takes an ID of a selected item (@BoundID) from the dropdownlist and gives a result (@BoundIdentifier) as follows: --- Running . ( @BoundID =
0
8752
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9401
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9176
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9113
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6702
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4519
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4784
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2635
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.