Hayden
Hayden

Reputation: 25

Putting Result From SQL Statement into Label

I have a label called lblHours, how can I put the result from the SQL statement below into this label?

  Dim SqlQuery1 As String = "SELECT SUM(Hours) FROM TimeSheet WHERE StaffID='" &
     cbStaffID.Text & "' AND TimeSheetMonth='" & cbMonth.Text & "'"

Upvotes: 0

Views: 4726

Answers (2)

Jim
Jim

Reputation: 6881

There are several different ways you can go about this.

This Microsoft article shows how to connect to an Access DB using OLEDB...

http://support.microsoft.com/kb/821765

That's one way to go about it. There are other ways to connect to an Access DB; you need to do some research on the basics.

Upvotes: 1

I kiet
I kiet

Reputation: 174

An Example

Dim con As SqlConnection = "your Connection String"
    Dim cmd As SqlCommand
    Dim query As String = "SELECT SUM(Hours) FROM TimeSheet WHERE StaffID='" &
 cbStaffID.Text & "' AND TimeSheetMonth='" & cbMonth.Text & "'"
    cmd = New SqlCommand(query, con)
    Try
    con.open
        Dim myreader As SqlDataReader = cmd.ExecuteReader()
        If myreader.Read() Then
            yourlable.text = myreader.GetValue(0)

        End If
        myreader.Close()
    Catch ex As System.Exception
        MsgBox(ex.Message)
    End Try
    con.Close()

Upvotes: 1

Related Questions