• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3909
  • Last Modified:

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

while(dr.Read())
{
      ar.Add(dr.GetValue(1));
}
((Label) Page.FindControl("lbl_1")).Text = ar[0].ToString("c");
((Label) Page.FindControl("lbl_2")).Text = ar[1].ToString("c");
etc...

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?
0
friskyweasel
Asked:
friskyweasel
  • 2
  • 2
1 Solution
 
É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):

((decimal)ar[0]).ToString("c");

Cheers!
0
 
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.....

thanks!



 
this was the syntax that worked for me
Convert.ToDecimal(ar[0]).ToString("c");
0
 
Éric MoreauSenior .Net ConsultantCommented:
friskyweasel,

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#!
0
 
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 : )
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now