Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

System.FormatException

ITAITA Posts: 463Member
I am developing a web form. The user types in the quantity of the product which they want into a textbox. I then convert this value(string) and perform calculations on it (double). The problem is, if nothing is entered in any or all of the textBoxes, then I get a System.FormatException. Can anyone tell me what I'm doing wrong?

Please help. Many thanks.

ITA :-)

Comments

  • LloydAZLloydAZ Posts: 5Member
    : I am developing a web form. The user types in the quantity of the product which they want into a textbox. I then convert this value(string) and perform calculations on it (double). The problem is, if nothing is entered in any or all of the textBoxes, then I get a System.FormatException. Can anyone tell me what I'm doing wrong?
    :
    : Please help. Many thanks.
    :
    : ITA :-)
    :
    The problem is that the form is trying to parse a blank string value that is not equivelent to a double thus causing your FormatException error.

    Try using an "if" statement to determine first of all if there is anything entered in the textbox, then use a try/catch to determine if the value is valid.

    Example:

    if(textbox1.Text !="")
    {
    try
    {
    dblTemp = Double.Parse(textbox1.Text);
    }
    catch (FormatException)
    {
    MessageBox.Show("Error Parsing textbox1", "Error");
    }
    }
  • mumpizmumpiz Posts: 75Member
    : : I am developing a web form. The user types in the quantity of the product which they want into a textbox. I then convert this value(string) and perform calculations on it (double). The problem is, if nothing is entered in any or all of the textBoxes, then I get a System.FormatException. Can anyone tell me what I'm doing wrong?
    : :

    ITA is right, but go a step further and inform user of error, e.g. FormatException, IOException etc, in try block do Your stuff, in catch block do handling of Exception. All Exceptions are from System.Exception so You will be able to get info You can pass on to user.

    Example
    if(textbox1.Text !="")
    {
    try
    {
    dblTemp = Double.Parse(textbox1.Text);
    }
    catch (Exception ex)
    {
    textbox1.Text = ex.message; // ex.ToString();
    }
    }

    Also check online help, Youll find good samples


Sign In or Register to comment.