Results 1 to 10 of 10

Thread: "Input string was not in a correct format"

  1. #1
    Join Date
    Mar 2005
    Location
    Hampshire
    Posts
    432
    Thanks
    4
    Thanked 3 Times in 3 Posts

    Default "Input string was not in a correct format"

    Does anyone have any idea on this, I have the following code in c#

    Code:
    value = Convert.ChangeType(value, property.PropertyType);
    value and property are recieved as
    Code:
    PropertyInfo property,
    object value
    value is an object

    property.PropertyType is a type

    Everything worked fine but is failing when value = "1234.56" and the property.PropertyType is an int.

    It is part of some code that I have to dynamically load data into a class from a csv file, using attributes to define what type of data to insert etc.

    Just chill

  2. #2
    Join Date
    Feb 2004
    Posts
    4,877
    Thanks
    2
    Thanked 134 Times in 113 Posts

    Default

    "1234.56" is not a (stringed) integer, integers have no decimal places; try a double/decimal/float.
    Warren Ashcroft
    Red Fox UK Limited - Pioneers in Internet Technology
    http://www.redfoxuk.com
    w.ashcroft [at] redfoxuk.com

    NOTE: Forum Private Messaging should not be used to contact staff with support queries.

  3. #3
    Join Date
    Mar 2005
    Location
    Hampshire
    Posts
    432
    Thanks
    4
    Thanked 3 Times in 3 Posts

    Default

    Thats the issue Warren.

    Basically I have some code that will import data from a csv dynamically. It uses attributes on the class parameters that define what column in the csv file belongs to what property. The data in the class is an int, however, in this case for some reason the data in the file is a decimal.

    Basically, all the importing is done via reflection of the class and its attributes. I then have code that does something similar to insert the data into a database creating the SQLCommand object and creating its parameters via attributes of the class.

    Just chill

  4. #4
    Join Date
    Feb 2004
    Posts
    4,877
    Thanks
    2
    Thanked 134 Times in 113 Posts

    Default

    So if the CSV can contain decimals, make sure property.PropertyType is a decimal, not an int?
    Warren Ashcroft
    Red Fox UK Limited - Pioneers in Internet Technology
    http://www.redfoxuk.com
    w.ashcroft [at] redfoxuk.com

    NOTE: Forum Private Messaging should not be used to contact staff with support queries.

  5. #5
    Join Date
    Mar 2005
    Location
    Hampshire
    Posts
    432
    Thanks
    4
    Thanked 3 Times in 3 Posts

    Default

    Thats the way that I'm heading at the moment, they cast it to an int when placing it into the database. Was hoping, someone would know of away to avoid doing it this was. As if the client gives the file with this issue in it for other int's means that I'm going to have to change the code again.

    Just chill

  6. #6
    Join Date
    Feb 2004
    Posts
    4,877
    Thanks
    2
    Thanked 134 Times in 113 Posts

    Default

    Quote Originally Posted by Tanzy View Post
    Thats the way that I'm heading at the moment, they cast it to an int when placing it into the database. Was hoping, someone would know of away to avoid doing it this was. As if the client gives the file with this issue in it for other int's means that I'm going to have to change the code again.
    I'd have thought Convert.ChangeType would have accepted the stringed double and spat out an integer, whats the error its giving? Maybe use a rounding function on all values (as doubles) to get integers if you don't want to work with doubles?

    Type conversion at the database level to integer would probably just chop off the decimals.
    Warren Ashcroft
    Red Fox UK Limited - Pioneers in Internet Technology
    http://www.redfoxuk.com
    w.ashcroft [at] redfoxuk.com

    NOTE: Forum Private Messaging should not be used to contact staff with support queries.

  7. #7
    Join Date
    Mar 2005
    Location
    Hampshire
    Posts
    432
    Thanks
    4
    Thanked 3 Times in 3 Posts

    Default

    Happy to just chop the decimals off, not worried about that. Must admit that I thought Convert.ChangeType would do the job as well. Ho... well....

    The error that I go was simply "Input string was not in a correct format", which is a bugger to track down what is going on when you catch the exception a couple of layers up in the code that caused it, and the code what was running at the time could have been a few different dynamic types. Not helping was the fact that this file has about 25 fields in it.

    Just chill

  8. #8
    Join Date
    Feb 2004
    Posts
    4,877
    Thanks
    2
    Thanked 134 Times in 113 Posts

    Default

    If property.PropertyType is an int, just round down (Math.Floor) to 0 decimal places then - that shoud sort it.
    Warren Ashcroft
    Red Fox UK Limited - Pioneers in Internet Technology
    http://www.redfoxuk.com
    w.ashcroft [at] redfoxuk.com

    NOTE: Forum Private Messaging should not be used to contact staff with support queries.

  9. #9
    Join Date
    Mar 2005
    Location
    Isle of Man
    Posts
    1,261
    Thanks
    3
    Thanked 23 Times in 23 Posts

    Default

    I don't use c#, but would guess that you can explicitly convert the value to an int?
    Code:
    int value = (int)1234.56;

  10. #10
    Join Date
    Mar 2005
    Location
    Hampshire
    Posts
    432
    Thanks
    4
    Thanked 3 Times in 3 Posts

    Default

    In normal cituations this would work Nick. However, because everything is dynamic here the type is only know at runtime.

    I ended up just making the property a decimal in the end.

    Just chill

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. HOWTO: Create a Temporary "Pre DNS Propagation" URL
    By Warren Ashcroft in forum Technical Support
    Replies: 5
    Last Post: 29th April 2008, 08:52 AM
  2. "Super" sort of cheap hosting option????
    By jigsawsrusdotcom in forum Sales and Service Feature Enquiries
    Replies: 2
    Last Post: 28th March 2006, 11:38 PM
  3. "String reference not set to " - Encrytion
    By jaimalchohan in forum ASP.NET
    Replies: 3
    Last Post: 28th November 2005, 08:15 PM
  4. Is there is a reason that redfox don't install "Helm Extensions" add onn
    By HostCan in forum Customer Feedback and Suggestions
    Replies: 1
    Last Post: 25th July 2005, 07:51 PM

Bookmarks

Posting Permissions

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