Convert to currency using ToString("c")

Posted on 2006-04-14
Last Modified: 2012-08-14
hi experts...seems like this should be really simple..i'm creating a datareader that will always return a known/static # of records...then looping thru it assigning values to an arraylist....i then want to turn around and manually assign the arraylist values to each of my dynamically created labels...something like this:
SqlDataReader dr = mycmd.ExecuteReader();
ArrayList ar = new ArrayList();

((Label) Page.FindControl("lbl_1")).Text = ar[0].ToString("c");
((Label) Page.FindControl("lbl_2")).Text = ar[1].ToString("c");

the values from the database are in decimal format by default....i need the value that i write to the label to be formatted as currency - everywhere else in the app, ToString("c") works fine...but using it in this context i get the error:
No overload for method 'ToString' takes '1' arguments
and the project won't compile....any idea why it's not working in this scenario?
Question by:friskyweasel
    LVL 69

    Accepted Solution

    Hi friskyweasel,

    This is because ar[0] returns an object type and an object cannot be converted to currency. Cast it as decimal first (my C# syntax may not be 100% good):



    Author Comment

    i got a 'specific cast is not valid' error with that one emoreau, but your answer to the question was 100% correct...that never even occurred to me - i was going in circles.....


    this was the syntax that worked for me
    LVL 69

    Expert Comment

    by:Éric Moreau

    I knew my syntax was probably having problems (because I am a VB guy) and I tried to cast it as you normally do in C#!

    Author Comment

    yup - shoot man that's not a problem at all - you came up with 95% of what I needed, which was the REASON it wasn't working...and you were correct - i just put the code i used out there in case anyone else stumbled across the question...

    i knew it had to be something simple, but was just drawing a complete blank! thanks again for your help : )

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on could not pull conte…
    ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    731 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now