Conversion from string "25." to type 'Double' is not valid.'

Shahab a 241 Reputation points
2022-10-02T07:27:29.043+00:00

Hi All
I Have 2 TextBoxes
I Want To multiplication Decimal Digit.
See My Code ...
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
LblResult.Text = Decimal.Parse(TextBox1.Text * TextBox2.Text)
End Sub

But I See this Error..
246667-1.jpg

How To Solve It.
Please Help me
Thanks all

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,570 questions
0 comments No comments
{count} votes

Accepted answer
  1. WayneAKing 4,921 Reputation points
    2022-10-03T10:26:21.863+00:00

    your codes is not work properly
    Please Trying Type 25.5 in TextBox1
    And See Error

    It works for us, so you need to show us what error you are getting.

    246975-test1.jpg

    Note also that the code you posted will throw an exception if either
    textbox is empty. So if you start to enter data in TextBox1 while
    TextBox2 is empty you will get an exception.

    Also, did you try the suggestion from Castorix31 to handle cases
    where the default decimal character is not a period? For example:

    LlblResult.Text = Decimal.Parse(TextBox1.Text,  
                  System.Globalization.CultureInfo.InvariantCulture) *  
                Decimal.Parse(TextBox2.Text,  
                  System.Globalization.CultureInfo.InvariantCulture)  
                         
      
    

    Note also that TryParse allows you to test for failure without
    getting an exception as happens with Parse.

    • Wayne
    1 person found this answer helpful.
    0 comments No comments

5 additional answers

Sort by: Most helpful
  1. Sreeju Nair 11,606 Reputation points
    2022-10-02T09:17:31.27+00:00

    try this

    lblResult.Text = Decimal.Parse(Textbox1.Text) * Decimal.Parse(Textbox2.Text)  
    

    Hope this helps

    1 person found this answer helpful.

  2. Castorix31 81,726 Reputation points
    2022-10-02T09:38:41.97+00:00

    This is often a language problem for the decimal separator

    You can use Decimal.TryParse with a culture for the 3rd parameter
    (it works for me with System.Globalization.CultureInfo.InvariantCulture on my french OS)

    0 comments No comments

  3. WayneAKing 4,921 Reputation points
    2022-10-02T18:06:33.353+00:00

    In addition to the good suggestions you have already received,
    note that there is another issue of which you need to be aware

    • that of implicit conversions.

    The result of your multiplication will be a Decimal. You then
    assign that result to what appears to be a string:

    lblResult.Text

    I'm assuming that lblResult is a Label.

    This requires a conversion from a Decimal to a string. That
    will happen implicitly if Option Strict is off. However if
    Option Strict is on, then you will get an error as this
    implicit conversion from a Decimal to a string will not be
    allowed. So you need to do the conversion explicitly, by
    calling the ToString method of the Decimal. For example:

    lblResult.Text = (Decimal.Parse(Textbox1.Text) *   
       Decimal.Parse(Textbox2.Text)).ToString  
      
      
    
    • Wayne
    0 comments No comments

  4. Shahab a 241 Reputation points
    2022-10-03T09:01:47.883+00:00

    thanks all
    your codes is not work properly
    Please Trying Type 25.5 in TextBox1
    And See Error