xTMx xTMx - 1 year ago 107
Vb.net Question

Program won't give me the right Sum

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)

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

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)
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?

Answer Source

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)
        End Using
    End Using

    ListBox1.DataSource = dt
    ListBox1.DataTextField = "PhoneName"
    ListBox1.DataValueField = "PhonePrice"

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
    End If

End Sub
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download