Returning Decimal values from database
One of my table column type is decimal(18,3). I use this column to store Weight values in kilograms, so it stores values in 0.500 (500 grams) or 5 (5 kilograms)
Now I am trying to get the value from this column.
I have a stored procedure which works correctly when I run it in SQL Query Analyser.
Then in my code behind file, I have:
decimal weight = MyCompany.WebModules.Products.Business.Products.Ge tBasketProductWeight(cartId, comID, true);
In Products.Data, I have:
public decimal GetBasketProductWeight(string cartID, int companyid, bool actualWeight)
{
int numAffected;
// create the parameters
.....
new SqlParameter("@TotalWeight", SqlDbType.Decimal, 9),
};
// set the values
......
parameters[3].Direction = ParameterDirection.Output;
RunDecimalProcedure("spv_Products_GetBasketWeight" , parameters, out numAffected);
return (decimal)parameters[3].Value;
}
Then in DbObject.cs, i changed the RunProcedure as follows:
protected decimal RunDecimalProcedure(string storedProcName, IDataParameter[] parameters, out int rowsAffected )
{
decimal result;
Connection.Open();
SqlCommand command = BuildIntCommand( storedProcName, parameters );
rowsAffected = command.ExecuteNonQuery();
result = Convert.ToDecimal((int)command.Parameters["ReturnValue"].Value);
Connection.Close();
return result;
}
However, I am still not getting the value in decimal. If the value returned from my database is 0.500, my code will return 0.
If the database returned value is 1, then my code returns 1.
So any idea how I can get return values like 0.500 (if the database has 0.500) and 5 (if the database has 5)
kind regards
|