my code is working but i think there's another way to do it using sql only. i am thinking reducing this code to much simpler. please help the code is getting the sum of stock table then subtract from transac table where same ricename then display the result. but if the there is null from the second query it is then display the result from the first query without having subtracting from the second query.
Dim commnd2 As New SqlCommand("SELECT RICETYPE.RICENAME, Sum(STOCK.NUMBEROFSTOCKS) AS SumOfNUMBEROFSTOCKS FROM SUPPLIER INNER JOIN (RICETYPE INNER JOIN STOCK ON RICETYPE.ID = STOCK.RICETYPEID) ON SUPPLIER.ID = STOCK.SUPPLIER GROUP BY RICETYPE.RICENAME", cn)
dr = commnd2.ExecuteReader()
If dr.HasRows Then
While dr.Read
Dim commnd3 As New SqlCommand("SELECT RICETYPE.RICENAME, Sum(TRANSAC.NUMBEROFSALES) AS SumOfNUMBEROFSALES
FROM RICETYPE INNER JOIN TRANSAC ON RICETYPE.ID = TRANSAC.RICETYPEID WHERE RICETYPE.RICENAME = '" & dr.Item(0) & "'
GROUP BY RICETYPE.RICENAME
", cn)
dr2 = commnd3.ExecuteReader()
If dr2.HasRows Then
While dr2.Read
dt.Rows.Add(dr2.GetValue(0), dr.GetValue(1) - dr2.GetValue(1), dr2.GetValue(1))
End While
Else
dt.Rows.Add(dr.GetValue(0), dr.GetValue(1), 0)
End If
End While
End If