Listbox Selected Value

B

Big E

I'm using ASP.Net and SQL Server.
I have a listbox looks up data from a table and fills the listbox.
I have a stored procedure that looks up the selected values and tries to
show them as selected in the listbox. My FOR NEXT LOOP gets stuck on one
record in the datatable. There are 4 records in the datatable and only the
first one is selected. It never moves past the first record.

MyCommand.Fill(dsCom, "tblCommRealtorAssociation")

dtCom = dsCom.Tables("tblCommRealtorAssociation")

Dim i As Integer

For i = 0 To dtCom.Rows.Count - 1

lstCommunities.SelectedValue =
dsCom.Tables("tblCommRealtorAssociation").Rows(0)("CommunityID")

Next
 
A

Adam Barker

I think you should be referencing the variable 'i' in the Rows indexer as
follows:

Dim i As Integer

For i = 0 To dtCom.Rows.Count - 1

lstCommunities.SelectedValue =
dsCom.Tables("tblCommRealtorAssociation").Rows(i)("CommunityID")

Next
 
B

Big E

You are right. Thanks.
How can I set more than one selected value on a listbox. It only sets the
last one in the dataset.

Thanks.

Big E
 
A

Adam Barker

Set

ListBox1.SelectionMode = ListSelectionMode.Multiple

and

ListBox1.Items[0].Selected = true

which means you may have to slightly rework your database access code.
 

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,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top