SQL type money to decimal HELP!
-
Hi, as simple as it sounds i'm having difficulties to achieve this. I have tried several ways but none of them works. This a part of my code: decimal amount = reader.GetSqlMoney(4).ToDecimal(); subtotal += amount ; What happens is that the value returned every time from my first line of code is zero. Also when i tried for testing purposes to give that value to a control's text property it works fine. Thanks
-
Hi, as simple as it sounds i'm having difficulties to achieve this. I have tried several ways but none of them works. This a part of my code: decimal amount = reader.GetSqlMoney(4).ToDecimal(); subtotal += amount ; What happens is that the value returned every time from my first line of code is zero. Also when i tried for testing purposes to give that value to a control's text property it works fine. Thanks
I would do it like this:
decimal amount = (decimal)rdr["MoneyColumnName"];
Just cast to
decimal
and forget aboutmoney
in the C# side of things. That's the way I have always done it and I have had no issues. After all, in SQL Servermoney
is actually adecimal
with a specific precision.Luis Alonso Ramos Intelectix Chihuahua, Mexico My Blog!
-
I would do it like this:
decimal amount = (decimal)rdr["MoneyColumnName"];
Just cast to
decimal
and forget aboutmoney
in the C# side of things. That's the way I have always done it and I have had no issues. After all, in SQL Servermoney
is actually adecimal
with a specific precision.Luis Alonso Ramos Intelectix Chihuahua, Mexico My Blog!
i'm still getting zero as a result. do you think anything else could be wrong that i should double-check?
-
i'm still getting zero as a result. do you think anything else could be wrong that i should double-check?
Well, the obvious things. Is your query returning the value of a money column? If you execute your query in SQL Management Studio, do you get the correct value? Could you show more code (creating the command, the parameters, the query or stored procedure, and so on)?
Luis Alonso Ramos Intelectix Chihuahua, Mexico My Blog!
-
i'm still getting zero as a result. do you think anything else could be wrong that i should double-check?
Do you close your Database Connection prior reading the DataReader? Did you check the HasRows property for your DataReader? Did you check while (reader.Read()) before casting the values? example: conn.Open(); SqlDataReader reader = command.ExecuteReader(); if (reader.HasRows) { while (reader.Read()) { // your castings here } } reader.Close(); conn.Close();