Listbox Selected Value

  • Thread starter Thread starter Big E
  • Start date Start date
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
 
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
 
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
 
Set

ListBox1.SelectionMode = ListSelectionMode.Multiple

and

ListBox1.Items[0].Selected = true

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