mercredi 20 janvier 2016

cmd.ExecuteScalar return 1 when there is not data

I have this sql query to count data in a table

SELECT COUNT(*) as cn FROM mytable WHERE Item_id=3 AND service_id=20 
AND Min_Qty=1 AND Max_Qty=1  

and i used this to check

dim iCount as integer = 0
Using cmd As New SqlCommand(qb.GetQuery, conn, sqlTrans)
      iCount = Convert.ToInt32(cmd.ExecuteScalar().ToString())
End Using

but count has 1 when there is no data at all in the table

i had to use this to check

 Using cmd As New SqlCommand(qb.GetQuery, conn, sqlTrans)
    Using dr As SqlDataReader = cmd.ExecuteReader()
       iCount = If(dr.Read, 0, 1)
    End Using
  End Using

before i got count to be zero

what could be the problem?

Aucun commentaire:

Enregistrer un commentaire