Hi All

I have a listview attached to a dbase app I am working on, the last column has a price value and I would like to create a total value for this column in a label or text box below the listview control.

I was searching back the old posts looking for some pointers or a code snippet that would do this, but was unable to find anything. i'm using vb 2008.

Any help appreciated

Thanks

kanababa

Try this,

1. Populate ListView.
2. Assign the result of aggregate sql function (select sum(price) from tableName) into Label's Text property.

thx adatapost for ur reply. i actually have two labels to fill with data. i followed the way u told me to fill the listview and label.text property. but it works for one label. i write one sql statement and want to apply two sum(s) in two different label controls from the same sql statement. down below is the code i write. can u please take a look and do something about it ??

Private Sub LoadTransactions()

        Dim Reader As OleDbDataReader

        CMD.Connection = CN 'connection string definer in a module
        CMD.CommandText = "SELECT AccTransaction.Dbt AS Debit, AccTransaction.Cdt AS Credit  " + _
                                           "FROM AccTransaction " 
        CN.Open()
        Reader = CMD.ExecuteReader
        lv_Trns.Clear()
        Dim i As Integer
        For i = 0 To Reader.FieldCount - 1
            lv_Trns.Columns.Add(Reader.GetName(i), 110, HorizontalAlignment.Center)
        Next
        While Reader.Read
            Dim LI As New ListViewItem
            LI.SubItems.Add(Convert.ToString(Reader.Item("Debit")))
            LI.SubItems.Add(Convert.ToString(Reader.Item("Credit")))
            lv_Trns.Items.Add(LI)
        End While
        CN.Close()

        'Populate Labels in Transactions Group Box
        CMD.CommandText = "SELECT DISTINCTROW SUM(Dbt), SUM(Cdt) " + _
                          "FROM AccTransaction"
        CN.Open()
        Reader = CMD.ExecuteScalar
        lbl_TranDbt.Text = [B]????[/B]
        lbl_TranCdt.Text = [B]????[/B]
        CN.Close()
    End Sub

Edited 6 Years Ago by __avd: Added [code] tags. Encase your code in: [code] and [/code] tags.

ExecuteScalar() is used to retrieve a single value. Use ExecuteReader() method.

Private Sub LoadTransactions()
       ....
        Reader.Close()
        CN.Close()

        'Populate Labels in Transactions Group Box
        CMD.CommandText = "SELECT  SUM(Dbt), SUM(Cdt) FROM AccTransaction"
        CN.Open()
        Reader = CMD.ExecuteReader()
        IF Reader.Read() Then
           lbl_TranDbt.Text = Reader.GetString(0)
           lbl_TranCdt.Text = Reader.GetString(1)
       End If
        Reader.Close()
        CN.Close()
    End Sub

Thanks adatapost.

But when I run my project with the code you privide, it gives an Error -----
"InvalidCastException was Unhandled" "Specified cast is not valid."

This article has been dead for over six months. Start a new discussion instead.