1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

.NET C#- String to Decimal Conversion - Format exception

Discussion in 'Software Development' started by GillIT, Oct 6, 2003.

Thread Status:
Not open for further replies.
  1. GillIT

    GillIT Thread Starter

    Joined:
    Jul 17, 2003
    Messages:
    53
    Hey everyone,

    The code is supposed to take a webservice output which is a string and convert it to a currency string. The only way I learned to get it to work is to convert it to a decimal then convert it back to a string as currency string. This worked fine with all the other webservices, except for just this one. For some reason it doesn't like to convert the string to a decimal. It works fine as long as I do not convert it but then it leaves a lot of following significant digits that I do not want. I have tried copy/pasting the solution directing into the decimal conversion and works fine. I am at a lost what the problem is. I can store it in a string, but cannot convert it to decimal.



    Here is the error I received:
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.FormatException: Input string was not in a correct format.

    Here is my code:
    string strTest2 = tk.webserviceX("aaa", "bbb");
    decimal x;
    string y;
    strTest2 = "00123.321";
    strTest2 = tk.webserviceX("aaa", "bbb");
    x = Convert.ToDecimal(strTest2);
    y = x.ToString("C");
    lblError1.Text = y;

    The error is line:
    x = Convert.ToDecimal(strTest2);



    Thanks.
     
  2. Guy

    Guy

    Joined:
    Feb 19, 1999
    Messages:
    260
    I would love to help but I am unfamiliar with tk.webserviceX("aaa", "bbb"); and I cannot find it in the docs.

    Have you tried debugging and stepping through the program to make sure that all values are as expected?
     
  3. Gibble

    Gibble

    Joined:
    Oct 9, 2001
    Messages:
    27,087
    what is the value of strTest2 immediately before the line "x = ..."
     
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/169900

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice