Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this SUM() query correct?

   string sql2 = "SELECT SUM(fyringsolje) FROM Informasjon WHERE fyringsolje='Kull: 2,42 kg';";
         SqlCommand myCommand2 = new SqlCommand(sql2, conn);
         kullTotaltLabel.Text = "" + (int)myCommand2.ExecuteScalar();

I don't see the text of the label changing, i am afraid the query cannot be correct. I want to make sure the query is correct!

like image 353
javing Avatar asked Jan 29 '26 23:01

javing


2 Answers

It cant be right. In the same select statement you are comparing the fyringsolje field to Text so that suggest that the value is a text field and you cant apply sum on it

like image 75
Fadrian Sudaman Avatar answered Feb 01 '26 12:02

Fadrian Sudaman


I doubt that that you want the ; at the end of the query... but just not seeing the label changing suggests that if something's going wrong, you're swallowing any exception that's being thrown.

Before you fix the query, fix the design so that when errors occur, you get to find out about them - otherwise you'll never be confident about what's happened.

I'd also change the final line to:

kullTotaltLabel.Text = myCommand2.ExecuteScalar().ToString();
like image 36
Jon Skeet Avatar answered Feb 01 '26 11:02

Jon Skeet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!