Q:

Read data from MySQL to Datagridview

I am trying to read data from MySQL and show on the Datagridview with code as below:
Dim totalManagerQuantity As Integer
Dim totalStaffQuantity As Integer
Dim totalProductionQuantity As Integer
Dim totalSalesManager As Double
Dim totalSalesStaff As Double
Dim totalSalesProduction As Double
        sqL = "SELECT TDate, InvoiceNo,
                  (
                    SELECT TD.Quantity
                    FROM TransactionDetails as TD
                    WHERE TD.InvoiceNo = T.InvoiceNo
                    AND TD.ProductNo = 1
                  ) as ManagerQuantity,
                  ( 
                    SELECT TD.ItemPrice
                    FROM TransactionDetails as TD
                    WHERE TD.InvoiceNo = T.InvoiceNo
                    AND TD.ProductNo = 1
                  ) as ManagerPrice,
                  (
                    SELECT TD.Quantity
                    FROM TransactionDetails as TD
                    WHERE TD.InvoiceNo = T.InvoiceNo
                    AND TD.ProductNo = 2
                  ) as StaffQuantity,
                  ( 
                    SELECT TD.ItemPrice
                    FROM TransactionDetails as TD
                    WHERE TD.InvoiceNo = T.InvoiceNo
                    AND TD.ProductNo = 2
                  ) as StaffPrice,
                  ( 
                    SELECT TD.Quantity
                    FROM TransactionDetails as TD
                    WHERE TD.InvoiceNo = T.InvoiceNo
                    AND TD.ProductNo = 3
                  ) as ProductionQuantity,
                  ( 
                    SELECT TD.ItemPrice
                    FROM TransactionDetails as TD
                    WHERE TD.InvoiceNo = T.InvoiceNo
                    AND TD.ProductNo = 3
                  ) as ProductionPrice
           FROM Transactions as T
           WHERE TDate BETWEEN '" & frmFilterReportInvoice.dtpStartDate.Value.ToString("MM/dd/yyyy") & "'
           AND '" & frmFilterReportInvoice.dtpEndDate.Value.ToString("MM/dd/yyyy") & "'
           AND StaffID = '" & lblTempStaffID.Text & "'"
        ConnDB()
        cmd = New MySqlCommand(sqL, conn)
        dr = cmd.ExecuteReader

        dgvReportInvoice.Rows.Clear()
        dgvTotalQuantity.Rows.Clear()
        totalManagerQuantity = 0
        totalStaffQuantity = 0
        totalProductionQuantity = 0
        totalSalesManager = 0
        totalSalesStaff = 0
        totalSalesProduction = 0

        Do While dr.Read = True

            dgvReportInvoice.Rows.Add(dr("TDate"), dr("InvoiceNo"), dr("ManagerQuantity"), dr("StaffQuantity"), dr("ProductionQuantity"))

            'Compute total manager, staff and production to Display
            totalManagerQuantity += dr("ManagerQuantity")
            totalStaffQuantity += dr("StaffQuantity")
            totalProductionQuantity += dr("ProductionQuantity")

            'Calculate the total amount per products
            totalSalesManager += (dr("ManagerPrice") * dr("ManagerQuantity"))
            totalSalesStaff += (dr("StaffPrice") * dr("StaffQuantity"))
            totalSalesProduction += (dr("ProductionPrice") * dr("ProductionQuantity"))

        Loop

But my code have an error when process calculation quantity (+=) as per below, will you help me how to solve my problem and what is the correct code, thanks.
P.S. The error come if there are no data on my table.
http://imgur.com/a/2BIAd
Regards,

A:

You should null check your values before summing: 
If not IsDBNull(dr("ManagerQuantity")) Then
  totalManagerQuantity += dr("ManagerQuantity")
End If

