problem when to do a sum by a date

mounir_az

Member
Joined
Jan 11, 2009
Messages
18
Programming Experience
1-3
I have a small problem when to do a sum by a date.

Here is my code.

Dim cmd As New SqlCommand("select sum(T_paiement.montant_paye) as montant from T_paiement where [date_peiement]= ('" & date_jour.value & "')", con)
dr = cmd.ExecuteReader
If dr.Read = True Then
On Error Resume Next
TextBox1.Text = dr(0)
End If.

I see it the following error

" Conversion from type 'DBNull' to type 'String' is not valid"

(date_peiement) is smalldatetime.
 
First up, if you're executing a query that returns a single value then you should call ExecuteScalar rather than ExecuteReader.

As for the error, it means that the field you're reading contains NULL, which would suggest that there were no matching records with a value in the column being summed.

Finally, don't use string concatenation to build SQL statements. Use parameters to insert variables into SQL statements. See the documentation for the SqlParameter class for an example.
 

Latest posts

Back
Top