none
Convert Label.Text to Integer RRS feed

  • Question

  • Plz help me

    I am try to count two months. but i am getting error; Input string was not in a correct format.

    I am try following codes:

    1. Dim intCalcu As Integer
             
    2. intCalcu = Convert.ToInt32(Label1.Text)
    3. Label1.Text = (DateTimePicker2.Value - DateTimePicker1.Value).Days / 30 & " Months "


    Monday, May 6, 2013 8:23 AM

Answers

  • You have to check that your strings can be converted to integers.  Use TryParse.
    Monday, May 6, 2013 8:36 AM
  • The standard conversion for VB to integers is CInt. The VB conversion methods are mostly (not always) optimized methods relying on the system methods.

    In your case the same as Convert.ToInt32 by the way, but simply shorter to write. 

    If it is a textbox, then don't rely that the user does it well. Put it inside a test (for instance a try catch an Isnumeric or a tryparse).

    Label1 has to be converted to a string, be aware for those DateTimePicker Values which are always correct the best to use is the ToString method with an empty parameter.  (Although the Cstr will go as well probably).


    Success
    Cor



    Monday, May 6, 2013 1:36 PM
  • you can use Val method to convert a string that starts with a number and contains some other junk data after that.

    Or you can use TryParse to check whether the label has a number of not

    If (Int32.TryParse(Label1.Text, intCalcu)) Then

    ' Use intCalcu here

    Else

    ' Label1 does not contain a valid number

    End If


    Muthukrishnan Ramasamy
    net4.rmkrishnan.net
    Use only what you need, Reduce global warming

    Monday, May 6, 2013 9:25 AM

All replies

  • You have to check that your strings can be converted to integers.  Use TryParse.
    Monday, May 6, 2013 8:36 AM
  • you can use Val method to convert a string that starts with a number and contains some other junk data after that.

    Or you can use TryParse to check whether the label has a number of not

    If (Int32.TryParse(Label1.Text, intCalcu)) Then

    ' Use intCalcu here

    Else

    ' Label1 does not contain a valid number

    End If


    Muthukrishnan Ramasamy
    net4.rmkrishnan.net
    Use only what you need, Reduce global warming

    Monday, May 6, 2013 9:25 AM
  • The standard conversion for VB to integers is CInt. The VB conversion methods are mostly (not always) optimized methods relying on the system methods.

    In your case the same as Convert.ToInt32 by the way, but simply shorter to write. 

    If it is a textbox, then don't rely that the user does it well. Put it inside a test (for instance a try catch an Isnumeric or a tryparse).

    Label1 has to be converted to a string, be aware for those DateTimePicker Values which are always correct the best to use is the ToString method with an empty parameter.  (Although the Cstr will go as well probably).


    Success
    Cor



    Monday, May 6, 2013 1:36 PM
  • Thanks for reply. 
    Saturday, May 11, 2013 9:44 AM