Reputation: 134
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
Reputation: 24136
You can do the multiplication within the SUM()
:
SELECT SUM(menu_price * menu_quantity) FROM cart WHERE email=@email
Upvotes: 1