Friday, February 24, 2012

format decimal

I want to format a number, a decimal, with 2 figures afther the "," but all I get is zeroes behind it. It works if I replace "cart.GetTotalWeight(cartId)"by 12.12 for example, 12,12 does not work... It driving me crazy

dim cart = New ShoppingCartDB()
TotalweightLbl.Text = string.Format("{0:n}",cart.GetTotalWeight(cartId))

the stored procedure...


CREATE Procedure CMRC_ShoppingCartTotalweigh
(
@.CartID nvarchar (50),
@.Totalweight decimal (5,2) OUTPUT

)
AS

SELECT
@.Totalweight = SUM(Products.Unitweight * ShoppingCart.Quantity)

FROM
ShoppingCart,
Products

WHERE
ShoppingCart.CartID = @.CartID
AND
Products.ProductID = ShoppingCart.ProductID
GO

the function...

Public Function GetTotalweight (ByVal cartID As String) As Decimal

' Create Instance of Connection and Command Object
Dim myConnection As SqlConnection = New SqlConnection ConfigurationSettings.AppSettings("ConnectionString"))
Dim myCommand As SqlCommand = New SqlCommand("CMRC_ShoppingCartTotalweigh", myConnection)

' Mark the Command as a SPROC
myCommand.CommandType = CommandType.StoredProcedure

' Add Parameters to SPROC
Dim parameterCartID As SqlParameter = New SqlParameter("@.CartID", SqlDbType.NVarChar, 50)
parameterCartID.Value = cartID
myCommand.Parameters.Add(parameterCartID)


Dim parameterTotalweight As SqlParameter = New SqlParameter("@.Totalweight", SqlDbType.decimal, )
parameterTotalweight.Direction = ParameterDirection.Output
myCommand.Parameters.Add(parameterTotalweight)

' Open the connection and execute the Command
myConnection.Open()
myCommand.ExecuteNonQuery()
myConnection.Close()

' Return the Total


If parameterTotalweight.Value.ToString() <> "" Then
Return parameterTotalweight.Value
Else
Return 0
End If

End Function

Dim parameterTotalweight As SqlParameter = New SqlParameter("@.Totalweight", SqlDbType.decimal, )

Tell it the precision and scale, you've chosen a decimal with a scale of 0 (The default).

No comments:

Post a Comment