Tuesday, March 13, 2012

Stuck

im building a ecommerce site (still)
any way i need to work out the shipping fee depending on the combined weight off the products.
each product has a column in the database called weight and at moment is an integer??

heres the sql statment

ALTER PROCEDURE GetTotalWeight
(@dotnet.itags.org.CartID varchar(50))
AS

DECLARE @dotnet.itags.org.Amount int

SELECT @dotnet.itags.org.Amount = SUM(Product.[Weight]*ShoppingCart.Quantity)
FROM ShoppingCart
INNER JOIN Product
ON ShoppingCart.ProductID = Product.ProductID
WHERE ShoppingCart.CartID = @dotnet.itags.org.CartID

IF @dotnet.itags.org.Amount IS NULL
SELECT 0
ELSE
SELECT @dotnet.itags.org.Amount
RETURN

heres the procedure!
Public Function GetTotalWeight() As Decimal
' Create the connection object
Dim connection As New SqlConnection(connectionString)

' Create and initialize the command object
Dim command As SqlCommand = New SqlCommand("GetTotalWeight", connection)
command.CommandType = CommandType.StoredProcedure

' Add an input parameter and supply a value for it
command.Parameters.Add("@dotnet.itags.org.CartID", SqlDbType.VarChar, 50)
command.Parameters("@dotnet.itags.org.CartID").Value = shoppingCartId

' Save the total amount to a variable
Dim amount As Decimal
connection.Open()
amount = command.ExecuteScalar()

' Close the connection
connection.Close()

' Return the amount
Return amount

If amount <= 1000 Then
amount = 3.46
ElseIf amount > 1000 < 1500 Then
amount = 4.45
ElseIf amount > 1500 <= 2000 Then
amount = 4.78
ElseIf amount > 2000 <= 4000 Then
amount = 7.2
ElseIf amount > 4000 <= 6000 Then
amount = 7.86
ElseIf amount > 6000 <= 8000 Then
amount = 8.96
ElseIf amount > 8000 <= 10000 Then
amount = 9.62
ElseIf amount > 10000 Then
amount = 11.21
End If

End Function

heres it setting it to the label
ShippingCostLabel.Text = String.Format("{0:c}", cart.GetTotalWeight())

obviousley this is wrong as the returned vale is always the combined weight so it obviousley bypasses the if statment!

can sum 1 plz help me on this as im baffled!
and if u really want to help out is there away to store the prices in a database so they can be modified?? but just getting an if stament like that 2 work would be great!

Thx in advance!First question is, what is shoppingCartId? Are you sure that is set to a valid shopping cart id?

Second, if you run the query in Query Analyzer, do you get the proper result there?

Regarding your final question about storing prices in a database so they can be modified, yes there is. Any data in a database can be modified, assuming that you have permissions to do so. You can use an UPDATE statement to change them.

Is that what you meant?

Don
Yep the sql statement works it calculates the weight and passes it on!

however its not useing the if statment after

return amount its just bypassing that big if statment how do u get it so it recaculates the amount varible using that big ass if statment??

what i meant with the database editing is how would u go about it cos u would have 2 compare the weight against a column in the databse?

say the column in the database

(weight int 4) (price money)
1000 £3.50
2000 £6

and the combined weight off the product was say 1500 so that would be the price for the 2000 row so how would u do that?? in the sql statemmnet i assume but i only now how to recive stuff if its precise.

Is that clearer??
can any1 help?? dont care about the database bit but how do i get that if statment working? thx!

0 comments:

Post a Comment