title: How to return the result of a postgresql function in c#? Console output empty
body: <p>I have the problem that i have a function in postgresql that calculates two integer and should return the result to the c# (npgsql) conosle and i don't know where my mistake is, because the debugger doesn't say anything to me which is helpful.</p>
  
  <p>so first of all the code of c# and of the function.</p>
  
  <pre><code>                ...
              cmd.Parameters["x"].Value = 20;
              cmd.Parameters["y"].Value = 22;
              connection.Open();
  
              if (connection.State == System.Data.ConnectionState.Open) {
                  //Console.WriteLine(cmd.Parameters["x"].Value);
                  command.ExecuteNonQuery();
                  Console.WriteLine(cmd.Parameters["sum"].Value);
  
              }
  </code></pre>
  
  <p>and now the code of the DB:</p>
  
  <pre><code>CREATE OR REPLACE FUNCTION t2(
      IN x integer,
      IN y integer,
      OUT sum integer)
    RETURNS integer AS
  $BODY$BEGIN
      sum := x + y;
      INSERT INTO  t2 (x, y, sum) values (x, y, sum);
  END
  </code></pre>
  
  <p>So when i try to run it,</p>
  
  <pre><code>Console.WriteLine(cmd.Parameters["sum"].Value);
  </code></pre>
  
  <p>will be empty and the ["sum"].Value is NULL. What am I doing wrong? Am I right, that when I say that "sum" is an OUT variable, I do not need a return?</p>
  
  <p>Please help.</p>
  
  <p><strong>SOLVED, thank you to all! @Patrick gave me the right answer:</strong>
  use ExecuteScalar() instead of ExecuteNonQuery() </p>