Convert to currency using ToString("c")

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?
Who is Participating?
Éric MoreauSenior .Net ConsultantCommented:
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):


friskyweaselAuthor Commented:
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
Éric MoreauSenior .Net ConsultantCommented:

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#!
friskyweaselAuthor Commented:
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 : )
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.