user3333278
user3333278

Reputation: 9

I need to calculate and the display the sum of the total sales in C#

I want to calculate the SUM of the total sales by Staff Id and I would also like to calculate the total sales for the month in C# but it keeps saying that my Input string is wrong.

here is the code for the total sales by Staff ID

 command.CommandText = "SELECT SUM (PriceSold) FROM SALELINE where SALELINE.SoldBy = '" +  comboBox10.SelectedItem.ToString() + "' ";
        string cmd = command.CommandText;
        double result = Convert.ToDouble(cmd);
        textBoxSalesStaffOutput.Text = Convert.ToString(result);

here is the code for the sales by month

 command.CommandText = " SELECT SUM (SaleTotalValue) FROM SALES where SALES.SaleDate BETWEEN '" + textBoxDate1.Text + "' AND '" + textBoxDate2.Text + "'";
        string cmd = command.CommandText;
        textBoxSalesMonthOutput.Text = Convert.ToString(cmd);

Any Ideas of what to do.

Upvotes: 0

Views: 312

Answers (3)

Charls
Charls

Reputation: 319

Your are not executing the command object. You need to use

double result = Convert.ToDouble(cmd.ExecuteScalar());

Upvotes: 1

Luke Marlin
Luke Marlin

Reputation: 1398

You are not executing your commands. You can execute them via ExecuteScalar as they will only return 1 row and 1 column. Here is an example for your first query :

double result = Convert.ToDouble(cmd.ExecuteScalar());

As mentioned in comments by @BabakNaffas, consider using parameters :

command.CommandText = "SELECT SUM (PriceSold) FROM SALELINE where SALELINE.SoldBy = @vendor ";
command.Parameters.AddWithValue("@vendor", comboBox10.SelectedItem.ToString());

Upvotes: 1

Chris Ballard
Chris Ballard

Reputation: 3769

Use command.ExecuteScalar to execute the command and retrieve a single result.

Upvotes: 0

Related Questions