Private Sub Form16_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim sqlquery As String = "SELECT SUM(price) AS totalprice FROM expenses"
        Dim sqlcommand As New OleDbCommand
        With sqlcommand
            .CommandText = sqlquery
            .Connection = conn
            .ExecuteNonQuery()
        End With
        Label2.Text= 'totalprice that is SUM(price)
    End Sub

    i want to display sum(price) in label2...how to do this plz help..
Private Sub Form16_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Dim strAccessConn as string = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=database.MDB"
        Dim sqlquery As String = "SELECT SUM(price) AS totalprice FROM expenses"

        Dim myDataSet As DataSet = new DataSet()
        Dim myAccessConn As OleDbConnection = Nothing
        Try
             myAccessConn = new OleDbConnection(strAccessConn)
             Dim sqlcommand As New OleDbCommand(sqlquery,myAccessConn)
             Dim myDataAdapter as OleDbDataAdapter = new OleDbDataAdapter(sqlcommand)

             myAccessConn.Open()
             myDataAdapter.Fill(myDataSet)
             myAccessConn.Close()


        Dim q = (From c In myDataSet.Tables(0).AsEnumerable
                Select New With {.totalprice = c.Field(Of Decimal)("totalprice")}).FirstOrDefault

        If q IsNot Nothing Then
            Label2.Text = q.totalprice.ToString()
        End If


        Catch
        End Try






    End Sub

Edited 2 Years Ago by xerohomicide

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