Your SQL query is returning a NULL when the table is empty. You cannot cast this return value to an int
, which is why C# is throwing an error.
If you're using SQL Server, you can check for NULL and replace with 0 in the database.
Select Isnull(Sum(Cena), 0) From Ksiazki
You can also use Coalesce
, which is the more general form of Isnull
(and is actually part of the SQL standard).