Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. SQL type money to decimal HELP!

SQL type money to decimal HELP!

Scheduled Pinned Locked Moved C#
databasetestingbeta-testinghelp
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Phivos Stylianides
    wrote on last edited by
    #1

    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

    L 1 Reply Last reply
    0
    • P Phivos Stylianides

      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

      L Offline
      L Offline
      Luis Alonso Ramos
      wrote on last edited by
      #2

      I would do it like this:

      decimal amount = (decimal)rdr["MoneyColumnName"];
      

      Just cast to decimal and forget about money 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 Server money is actually a decimal with a specific precision.

      Luis Alonso Ramos Intelectix Chihuahua, Mexico My Blog!

      P 1 Reply Last reply
      0
      • L Luis Alonso Ramos

        I would do it like this:

        decimal amount = (decimal)rdr["MoneyColumnName"];
        

        Just cast to decimal and forget about money 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 Server money is actually a decimal with a specific precision.

        Luis Alonso Ramos Intelectix Chihuahua, Mexico My Blog!

        P Offline
        P Offline
        Phivos Stylianides
        wrote on last edited by
        #3

        i'm still getting zero as a result. do you think anything else could be wrong that i should double-check?

        L H 2 Replies Last reply
        0
        • P Phivos Stylianides

          i'm still getting zero as a result. do you think anything else could be wrong that i should double-check?

          L Offline
          L Offline
          Luis Alonso Ramos
          wrote on last edited by
          #4

          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!

          1 Reply Last reply
          0
          • P Phivos Stylianides

            i'm still getting zero as a result. do you think anything else could be wrong that i should double-check?

            H Offline
            H Offline
            Herman T Instance
            wrote on last edited by
            #5

            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();

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups