Binding data to DropDownList...

R

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
 
M

mark4asp

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();
 
L

Lloyd Sheen

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
 
R

RAB

What would one set Listbox1 DataValueField equal to, if one used the
ammended select statement?

Thanks,
RABMissouri2007
 
R

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
 
L

Lloyd Sheen

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
 

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,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top