locked
why not ???? RRS feed

  • Question

  • int Credits = Convert.ToInt32(datatbl5.Rows[0][0].ToString());

    when i pass it nothings it gives error "input string was not in correct format" ?

    Monday, December 23, 2019 8:14 AM

All replies

  • This error indicates that the value "datatbl5.Rows[0][0]" is null or is not a string that contains the number to convert.
    For example, you can't convert the "abc" to int by using the Convert.ToInt32 method.
    You can use Int32.TryParse method, and it returns a value that indicates whether the operation succeeded.
    Here is a simple code example.

    int Credits;
    bool s = Int32.TryParse(table.Rows[0][0].ToString(), out Credits);

    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, December 24, 2019 7:37 AM
  • To add to Daniel's reply, if it can't Parse the data (and so it returns a false), the value in Credits will be set to zero.

    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Wednesday, December 25, 2019 12:44 AM
  • Hi,
    Has your problem been solved? If it is resolved, we suggest that you mark it as the answer. So it can help other people who have the same problem find a solution quickly.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, January 10, 2020 5:23 AM