0

Let me start by posting my code first:

ExecuteScalar method:

public T ExecuteScalar<T>(string sql, CommandType commandType, List<NpgsqlParameter> parameters)
{
    using (NpgsqlConnection conn = Konekcija_na_server.Spajanje("spoji")) 
    {
        return Execute<T>(sql, commandType, c =>
        {
            var returnValue = c.ExecuteScalar();
            return (returnValue != null && returnValue != DBNull.Value && returnValue is T)
             ? (T)returnValue 
             : default(T); 
        }, parameters);

    }        
}

Execute method:

 T Execute<T>(string sql, CommandType commandType, Func<NpgsqlCommand, T> function, List<NpgsqlParameter> parameters)
    {
        using (NpgsqlConnection conn = Konekcija_na_server.Spajanje("spoji")) 
        {
            using (var cmd = new NpgsqlCommand(sql, conn))
            {
                cmd.CommandType = commandType;
                if (parameters.Count > 0 ) 
                {
                    foreach (var parameter in parameters) 
                    {
                        cmd.Parameters.AddWithValue(parameter.ParameterName,parameter.Value);
                    }
                }
                return function(cmd);
            }

        }

    }

Call of ExecuteScalar method:

komanda = "begin;select count(*) from radni_sati where ime=@ime and prezime=@prezime" +
                      " and (dolazak is not null and odlazak is not null and sati_rada is not null) and napomena='' ;commit;";
            listaParametara.Add(new NpgsqlParameter { ParameterName = "@ime", Value = ime });
            listaParametara.Add(new NpgsqlParameter { ParameterName = "@prezime", Value = prezime });
            var nePrazni_redovi=instanca.ExecuteScalar<int>(komanda, CommandType.Text, listaParametara); 
            listaParametara.Clear();

Now my problem is when I call ExecuteScalar().

For some reason my ExecuteScalar always returns 0 as result and that can't be coz I tested it as a normal query in PSQL Shell and it always returns values>0 when I call legit query that has to return normal value.

First time It enters ExecuteScalar after a call, returns a returnValue from lamba operator and for example its 16, then when it goes to Execute function, somehow it returns 0 and I dont understand why, coz main thing I need ExecuteScalar for is to return count(*) value out as an int.

Marko Petričević
  • 333
  • 2
  • 9
  • 20
  • 2
    Please check the return value of c.ExecuteScalar. If this is not the expected type, then default(T) (in this case default(int)) is returned, which evaluates to zero (even if the SQL statement returns another value). – Ralf Bönning Oct 08 '17 at 09:34
  • @RalfBönning could you post that as an answer with an example of what ur trying to say, coz if I change `T` to `int` on `default` or `returnValue` I have to change whole type of method to `int` for method to work – Marko Petričević Oct 08 '17 at 10:17
  • Just remove the `is T` test. If the cast fails an exception should be thrown... But that should be OK as an exceptional circumstance (you're not getting the type you expect). Your db provider might be returning a `short` or a `long` or a `BigInteger`, hell maybe it's returning a `float` containing a whole number... none of which would be T, for example, an `int` so the test fails and returns default(T) which for integral types is zero. – pinkfloydx33 Oct 08 '17 at 18:21
  • @pinkfloydx33 Did it, looks like this now: `return (returnValue != null && returnValue != DBNull.Value)` and I get `System.InvalidCastException: 'Specified cast is not valid.'` – Marko Petričević Oct 08 '17 at 18:37
  • @IvanMilosavljevic ExecuteScalar method is called in 3rd paragraph of code. `returnValue` in my test query is 29, and type is `long` – Marko Petričević Oct 08 '17 at 20:55
  • @IvanMilosavljevic In period im having this "stone around my hand", I never taught about comparing types....Thanks man, saved my day =) Post it as an answer – Marko Petričević Oct 08 '17 at 21:02
  • @MarkoPetričević no problem mate, I`m glad I could help you out. – Ivan Milosavljevic Oct 08 '17 at 21:09
  • @IvanMilosavljevic Take a reward for it by posting it as an answer, so we can remove the comments ;) – Marko Petričević Oct 08 '17 at 21:26

1 Answers1

2

can you tell us how are you calling ExecuteScalar? What type is T? Also, set breakpoint to: var returnValue = c.ExecuteScalar(); and check what type is returned after you step over that line (F10). In watch window of Visual Studio you should check Type column.

Ivan Milosavljevic
  • 839
  • 1
  • 7
  • 19