-1

I have got this code which SUM number(9,2) values in SQL DB, but the output is System.Data.SqlClient.SqlCommand.

I know I have to Convert it somehow but have no idea how?

Would you please suggest me something? Thank you very much for your time.

    conn.Open();
    SqlCommand sc = new SqlCommand("SELECT SUM(price) AS sumprice FROM client WHERE subkey="+key,conn);
    sc.ExecuteNonQuery();
    conn.Close();

    textBox20.Text = sc.ToString();
}
Blaze M
  • 200
  • 4
  • 16
  • I would suggest looking up when to use `ExecuteNonQuery` and when not too.. this is something that you can easily google for future reference – MethodMan Jul 22 '13 at 14:22

1 Answers1

12

what you are looking is ExecuteScalar() not ExecuteNonQuery().

int _result = Convert.ToInt32(sc.ExecuteScalar());

For proper coding

  • parameterize the value to avoid sql injection
  • use using statement for proper object disposal
  • use try-catch block to properly handle exceptions
John Woo
  • 258,903
  • 69
  • 498
  • 492