1

I have a table, and it has the Columns Quantity, Price and LineTotal. When I add something to the Cart, I add the Quantity and the Price of the product, and when the user visits the Cart page to view their cart, I want to re-calculate the LineTotal when they update the quantities for the items they've selected.

Now, my question is, should I re-calculate the LineTotal of each item in the cart using SQL (and if so, how?) or, should I do it in C# (And if so, what would be the best way to go about this?) To my surprise, I can't really seem to find anything on calculations in SQL - other than forums where people talk about it, but I have yet to see any code or documentation.

2 Answers 2

1

You should be able to use the arithmetic operators within SQL to generate your line totals. Reference here.

Sign up to request clarification or add additional context in comments.

Comments

1

Well if you wanted to you could change the LineTotal column to a computed column but there's nothing stopping you from doing it in C# before the Insert/update

Since it seems to be a fairly simple calculation its tough to really strongly go with one or the other, and its mostly up to preference

Computed Column sample

ALTER TABLE dbo.YourTable
   ADD LineTotalComputed AS (Quantity * Price) PERSISTED

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.