CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2006
    Posts
    28

    ToString format causes InvalidCastException

    I am using .ToString("#,##0.00") to format a number.
    I'm sure I have used this before but today it doesn't work.

    I want to format a System.Double value from the datatable like this 12345.6789 to 12,345.68

    Code:
    myString = dt.Rows(i)("column_name").ToString("#,##0.00")
    gives a System.InvalidCastException:

    Conversion from string "#,##0.00" to type 'Integer' is not valid.

    Any ideas?
    I'm using .NET Framework 3.5

    I'm planning to be spontaneous tomorrow

  2. #2
    Join Date
    Dec 2007
    Posts
    234

    Re: ToString format causes InvalidCastException

    Try:
    dt.Rows(i)("column_name").Value.ToString("#,##0.00")
    instead.

    -tg
    * I don't respond to private requests for help. It's not conducive to the general learning of others.-I also subscribe to all threads I participate, so there's no need to pm when there's an update.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help - how to remove eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to???
    * On Error Resume Next is error ignoring, not error handling(tm). * Use Offensive Programming, not Defensive Programming.
    "There is a major problem with your code, and VB wants to tell you what it is.. but you have decided to put your fingers in your ears and shout 'I'm not listening!'" - si_the_geek on using OERN
    MVP '06-'10

  3. #3
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: ToString format causes InvalidCastException

    You'd could use String.Format()

    Code:
    Code:
    String.Format(dt.Rows(i)("column_name").Value.ToString,{0:#,##0.00}
    Or something like that
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured