Getting sum in asp.net page

D

David C

I have the following code in a asp.net web page that should always return a
single value (sum). Is there a better way to do this so I just get the
result and not have to check for HasRows, etc.? Also, I am thinking of
making this a stored proc when we go into production. If so, would the
"read" syntax be the same? Thanks.

David

'Get cost of parts sold this month for jobs completed
strSQL = "SELECT SUM(PartsCost) As TotalPartsCost" & _
" FROM dbo.vw_RORatesByPC" & _
" WHERE YEAR(RepairCompleteDate) = " & intYear.ToString & _
" AND MONTH(RepairCompleteDate) = " & intMonth.ToString
cmdSelect = New SqlCommand(strSQL, conData)
dtr = cmdSelect.ExecuteReader()
If dtr.HasRows Then
While dtr.Read()
If Not IsDBNull(dtr("TotalPartsCost")) Then
dblTotalCost = dtr("TotalPartsCost")
End If
Exit While
End While
End If
dtr.Close()
 
S

Sam

If you expect just a single value return, use ExecuteScalar instead of
ExecuteReader
 
B

bruce barker

dblTotalCost = cmdSelect.ExecuteScaler();

note: this will throw an error if no rows.

-- bruce (sqlwork.com)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top