473,320 Members | 1,572 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Binding data to DropDownList...

RAB
I have the following code:

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

Function GetName() As System.Data.IDataReader
Dim connectionString As String = 'path to datasource
Dim dbConnection As System.Data.IDbConnection = New
System.Data.OleDb.OleDbConnection
(connectionString)

Dim queryString As String = "SELECT [Customer].[ID],
[Customer].[LName], [Customer].[FName] FROM
[Customer]"
Dim dbCommand As System.Data.IDbCommand = New
System.Data.OleDb.OleDbCommand
dbCommand.CommandText = queryString
dbCommand.Connection = dbConnection

dbConnection.Open
Dim dataReader As System.Data.IDataReader =
dbCommand.ExecuteReader

(System.Data.CommandBehavior.CloseConnection)

Return dataReader
End Function

Sub Page_Load(sender As Object, e As EventArgs)
Listbox1.datasource = GetName()
Listbox1.databind()
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 1191
On 12 Mar, 16:00, "RAB" <rabmisso...@yahoo.comwrote:
I have the following code:

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

Function GetName() As System.Data.IDataReader
Dim connectionString As String = 'path to datasource
Dim dbConnection As System.Data.IDbConnection = New
System.Data.OleDb.OleDbConnection
(connectionString)

Dim queryString As String = "SELECT [Customer].[ID],
[Customer].[LName], [Customer].[FName] FROM
[Customer]"
Dim dbCommand As System.Data.IDbCommand = New
System.Data.OleDb.OleDbCommand
dbCommand.CommandText = queryString
dbCommand.Connection = dbConnection

dbConnection.Open
Dim dataReader As System.Data.IDataReader =
dbCommand.ExecuteReader

(System.Data.CommandBehavior.CloseConnection)

Return dataReader
End Function

Sub Page_Load(sender As Object, e As EventArgs)
Listbox1.datasource = GetName()
Listbox1.databind()
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.DataSource = GetName();
Listbox1.DataValueField = "myValueName";
Listbox1.DataTextField = "myTextName";
Listbox1.DataBind();

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*********@yahoo.comwrote in message
news:11**********************@q40g2000cwq.googlegr oups.com...
>I have the following code:

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

Function GetName() As System.Data.IDataReader
Dim connectionString As String = 'path to datasource
Dim dbConnection As System.Data.IDbConnection = New
System.Data.OleDb.OleDbConnection
(connectionString)

Dim queryString As String = "SELECT [Customer].[ID],
[Customer].[LName], [Customer].[FName] FROM
[Customer]"
Dim dbCommand As System.Data.IDbCommand = New
System.Data.OleDb.OleDbCommand
dbCommand.CommandText = queryString
dbCommand.Connection = dbConnection

dbConnection.Open
Dim dataReader As System.Data.IDataReader =
dbCommand.ExecuteReader

(System.Data.CommandBehavior.CloseConnection)

Return dataReader
End Function

Sub Page_Load(sender As Object, e As EventArgs)
Listbox1.datasource = GetName()
Listbox1.databind()
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*********@yahoo.comwrote in message
news:11**********************@s48g2000cws.googlegr oups.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
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...
2
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. ...
0
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...
3
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%>"...
5
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"...
5
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);...
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...
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...
2
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 ::...
2
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.