Ariff Naj
Ariff Naj

Reputation: 134

How to sum 2 different column and sum it for different row

enter image description here

how to calculate menu_price * menu_quantity and total for all row here my code, this one only for one column, and without multiple with menu_quantity. when you see the table, you can guess that I want calculate total cost

string stmt = "SELECT SUM(menu_price) FROM cart Where email=@email";

try
{
    using (SqlConnection con = new SqlConnection(ConfigurationManager.
   ConnectionStrings["connectionString"].ConnectionString))
    {
        using (SqlCommand cmd = new SqlCommand(stmt, con))
        {
            cmd.Parameters.AddWithValue("@email", Session["email"].ToString());
            con.Open();
            int count = (int)cmd.ExecuteScalar();
            return count;
        }
    }
}
catch (Exception e)
{
    lbltotalitemcart.Text = e.ToString();
    return 0;
}

Upvotes: 0

Views: 50

Answers (1)

Peter B
Peter B

Reputation: 24136

You can do the multiplication within the SUM():

SELECT SUM(menu_price * menu_quantity) FROM cart WHERE email=@email

Upvotes: 1

Related Questions