I’m just beginning to transition to ASP.NET and C# from a largely Java / C++ / C background, and along the way, I’m finding myself often stumped with what seems like really simple problems.
For example, today, when writing a ASP.NET web service in C#, I had trouble casting a money value retrieved from a MSSQL database to a double. The following code would always report an error:
Have you found this post helpful?
If so, I'd appreciate if you could indicate so by pressing the Google Plus button below.
C#
double cost;
cost = (double) dr["cost"];
I also tried using a float, but that didn’t fix the problem either.
The _correct_ type to use in this case is actually Decimal, and I learned this only after a co-worker sent me a link to a document on the MSDN site that has a table showing all of the proper type conversions from one system to another. So, this code _does_ work:
C#
Decimal cost;
cost = (Decimal) dr["cost"];
You can find the table here:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconusingparameterswithdataadapters.asp
(Scroll about halfway down; you can’t miss it.)
Related posts:
8 Comments to “Casting from MSSQL money to C# double”
Post comment
Recommended Services
Recent Posts
- Fantastic new corporate themes for WordPress
- Vistaprint offers FREE t-shirts, too!
- 80+ “Your Ad Here” 125 x 125 banners
- 5 Minute Long Tail SEO Drill: More Traffic, Better SERPs
- iPhone and iPod Touch app statistics: OS adoption, purchasing rates
Recent Comments
- Neerav on Using JavaScript to validate one or more grouped checkboxes
- Jacob on Using JavaScript to validate one or more grouped checkboxes
- kaify on Using JavaScript to validate one or more grouped checkboxes
- JC Goldenstein on How To: Cloak your Affiliate Links for Free in Under 3 Seconds
- Bail Bonds Los Angeles on Amazon Web Services on Rails
Categories
- .net
- acoustic guitar
- affiliate marketing
- ajax
- amazon associates
- blogging
- books
- business ideas
- c#
- code igniter
- dealdotcom
- google adsense
- google adwords
- internet marketing
- iPhone
- javascript
- leadership
- make money online
- mortgage goal
- msn adcenter
- networking
- personal development
- php
- ppc
- ruby
- ruby on rails
- seo
- text-link-ads
- web development
- yahoo search marketing





Satish says:
I hv some problem in money data type of MSSQL i wt to when to use that data type and my problem is if i insert 12.58 then i wt when ever i retrive data i wt same figur not 12.5800 or something else
Replay Soon
Aditya Kumar says:
I hv some problem in money data type of MSSQL i wt to when to use that data type and my problem is if i insert 12.58 then i wt when ever i retrive data i wt same figur not 12.5800 or something else
filip says:
Use double for value
for sqldbtype use float
in database
use money
erik says:
You need to change (double) dr["cost"]; to Convert.ToDouble(dr["cost"];
erik says:
oops should be this Convert.ToDouble(dr["cost"]);
Dale says:
erik… you’re awesome. That worked for me. Not sure why money is 4 decimals long in SQL fields but thanks to you, I was able to convert my money field in SQL to a correctly US formated two decimal places. Thanks. Dale n Orlando, Florida
Searock says:
Thanks a lot, you saved me from a headache !
Phiqfzqy says:
Who do you work for?