Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The listbox is populated from a table in the database,it has more than 10 items.Some items in this listbox aren't being highlighted/chosen correctly(1st,2nd,3rd highlighted correctly,4th,5th Not,6th Yes,7th Not ....etc).
After choosing the item(s) they are displayed in a Label on the same page....everything works correctly except the items not being chosen correctly part. Any help will be appreciated!

Image link : http://i.imgur.com/Nmr1gG2.png?1[^]

What I have tried:

This is the listbox code:
VB
    Sub FillItemsList()
        Dim connectionString As String = "Data Source=.;Initial Catalog=Shop;integrated security=true"
        Dim dt As New DataTable()

        Using connection As New SqlConnection(connectionString)
            Dim sql As String = "SELECT PhoneName,PhonePrice FROM SmartPhones"
            Using adapter As New SqlDataAdapter(sql, connection)
                adapter.Fill(dt)
            End Using
        End Using

        ListBox1.DataSource = dt
        ListBox1.DataTextField = "PhoneName"
        ListBox1.DataValueField = "PhonePrice"
        ListBox1.DataBind()

    End Sub
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        Label2.Text = Request.QueryString("Name").ToString()

        If Not Page.IsPostBack Then
            FillItemsList()
        End If
        
    End Sub


    Protected Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
        sum = 0 'reset sum to 0
        For Each i As Integer In ListBox1.GetSelectedIndices
            Dim CurrentItem As ListItem = ListBox1.Items(i)
            sum = sum + CInt(CurrentItem.Value)
            Items1 = Items1 + " | " + CStr(CurrentItem.Text)
        Next
        Label3.Text = Items1
        Label1.Text = sum


    End Sub


Listbox asp code:
HTML
<asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple" autopostback="true"</asp:ListBox>
Posted
Updated 25-May-16 8:53am
v3
Comments
Suvendu Shekhar Giri 25-May-16 13:40pm    
What do you mean by "Highlighted correctly/incorrectly"?
xTMx9 25-May-16 13:51pm    
I added a new code if it makes things clearer about what is supposed to happen
xTMx9 25-May-16 14:03pm    
I uploaded an image here's the link: http://i.imgur.com/Nmr1gG2.png?1
xTMx9 25-May-16 13:50pm    
when i choose the first item from the listbox,it is correctly printed in the label but if i choose the 4th item ,it doesn't choose it instead chooses the 1st item(even if i choose the 7th item it instead chooses the 5th one) although choosing the first 3 items works and are placed in the Label!

1 solution

Based on the description in the comments, you have multiple records with the same value in the PhonePrice column.

When the form posts back to the server, only the value of the selected item(s) is sent; the server has no idea which of the multiple items with the same value you have selected. As a result, it selects the first item with the specified value.

And no, this is not something you can change. It's just how HTML forms work.

You'll need to make sure each item in your list has a unique value. Try using the primary key from your SmartPhones table.
 
Share this answer
 
Comments
xTMx9 25-May-16 15:04pm    
Now i Understand,but in addition to
ListBox1.DataTextField = "PhoneName"
ListBox1.DataValueField = "PhonePrice"
(i need the price elsewhere so i can't remove it)
how can i also get the ID(which is the PK) to be sent to the listbox?
Richard Deeming 25-May-16 15:06pm    
Add ID to your SELECT query, and then set ListBox1.DataValueField = "ID".
xTMx9 25-May-16 15:10pm    
It is working now but the sum is now incorrect since the value is ID and not the price
sum = sum + CInt(CurrentItem.Value)
Richard Deeming 25-May-16 15:13pm    
Yes; you'll have to query the database again to get the price.

Alternatively, concatenate the ID and price fields in the SQL query, and then split them out when you sum them:
Dim sql As String = "SELECT CAST(ID As varchar(10)) + '-' + CAST(PhonePrice As varchar(10)) As ID, PhoneName FROM SmartPhones"
...
ListBox1.DataValueField = "ID"
...
sum = sum + CInt(CurrentItem.Value.Split("-"c)(1))
xTMx9 25-May-16 15:18pm    
Now it works correctly! Thanks!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900