-1

I want to get the sum of the selected items in the listbox and display them in a label but i am always getting 0,i also want to put the selected items in another label too which is also not working.

Here is what the code look like:

Dim sum As Integer
Dim Items1 As String = "None"

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

    Dim connetionString As String = Nothing
    Dim connection As SqlConnection
    Dim command As SqlCommand
    Dim adapter As New SqlDataAdapter()
    Dim ds As New DataSet()
    Dim sql As String
    connetionString = "Data Source=.;Initial Catalog=Shop;integrated security=true"
    sql = "select PhoneName,PhonePrice from SmartPhones"
    connection = New SqlConnection(connetionString)

    connection.Open()
    command = New SqlCommand(sql, connection)
    adapter.SelectCommand = command
    adapter.Fill(ds)
    adapter.Dispose()
    command.Dispose()
    connection.Close()
    ListBox1.DataSource = ds.Tables(0)
    ListBox1.DataTextField = "PhoneName"
    ListBox1.DataValueField = "PhonePrice"
    ListBox1.DataBind()


End Sub

code where the display should happen:

    Protected Sub Button2_Click(sender As Object, e As EventArgs) Handles TotalPrice.Click
   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

Here is the page Design and the Page On the web Respectively:enter image description here enter image description here

PhoneName is of type varchar in database & PhonePrice is of type integer (Both Filled correctly).

ListBox code:

 <asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple" ></asp:ListBox>

What's the reason that the code won't give me the desired result?

xTMx
  • 67
  • 9
  • If you debug on the for each, at the "CurrentItem.Value", does it display the value or 0? – Aimnox May 23 '16 at 11:05
  • Set breakpoints, step through your code, inspect your variables. – CodeCaster May 23 '16 at 11:14
  • 1
    At first glance, it appears that your code to populate the ListBox should be run only `If Not Page.IsPostBack`. – Andrew Morton May 23 '16 at 11:56
  • I am currently on my phone, i will try it but how can i debug on currentItem.value?i didn't understand? I am still new to the listbox controls so i don't know much about it – xTMx May 23 '16 at 12:03

1 Answers1

1

What is happening is that when you click TotalPrice a postback is performed (What is a postback?). If you look at the ASP.NET page lifecycle you will see that the Load event happens before the postback event handling (e.g. your Sub Button2_Click).

So, you click the button, it runs the Me.Load handler and... your list is reset before the click handler gets a chance to run.

There is a property you can check to see if the page is running as a result of a postback: Page.IsPostBack.

So all you need to do is check it to see if you need to populate the list:

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

Private 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
Community
  • 1
  • 1
Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
  • This is something new to me and you just taught me that,thank you for your help!! Just one more thing,whenever i choose lg g5 or some items from the bottom of the list,they are not picked up and instead the first item is picked!! Any reason why this is happening?? – xTMx May 23 '16 at 20:33
  • Even when choosing 1 item at a time without multiselection, some items like the lg g5 in the above picture is not getting picked ad instead it is referring as if i picked the 1st item....For other items below in the list,Some are chosen correctly and others are having the same problem like the g5 one,although it is not choosing automatically the 1st item but another one from the list!! Any help will be appreciated.... – xTMx May 23 '16 at 21:58
  • @xTMx Now you need to debug it - which isn't always quite as simple as debugging a Windows Forms program. You'll have to do a bit of research. [Listbox in asp.net not getting selected items](http://stackoverflow.com/questions/16338246/listbox-in-asp-net-not-getting-selected-items) might help, even though it's in C#. – Andrew Morton May 24 '16 at 08:24