-2

I want to create a 3-Layer architecture Web application. but finding some issues like:

An exception of type 'System.FormatException' occurred in mscorlib.dll but was not handled in user code

I have tried int.Parse() method even its not working.

How do I handle this issue in ASP.NET 3Layer web application?

protected void btnDetails_Click(object sender, EventArgs e)
    {
        int id = int.Parse(txtId.Text);
        Product product = db.GetProducts().Single(x => x.productId == id);
        txtNmae.Text = product.name;
        txtPrice.Text = product.price.ToString();
    }
garfbradaz
  • 3,424
  • 7
  • 43
  • 70
hod
  • 7
  • 4

1 Answers1

1

You should use int.TryPrase() instead of int.Prase().

int.Parse() will throw an exception if the input string is not a valid integer, whereas int.TryPrase() will return true/false depending on whether the conversion is valid.

int.Parse()

This throws an exception.

string text = "abc";
int id = int.Parse(text);

but,

int.TryPrase()

This will return false.

string text = "abc";
bool result = int.TryParse(text, out int value);

Edit

The reason why your program throws an exception is that the text in your TextBox is not an integer value. The int.Parse() method will convert a string value to int if it's a valid integer value. Therefore, if the TextBox contains, for example, some characters like a, b etc, or even if it's an empty string, then the Parse() method cannot convert it to an integer and thus throws an exception complaining that your input string is not in a correct format.

The int.TryParse() method goes a step further. It tries to convert the value to an integer, and if successful, returns true, and gives you the converted value in int format in the out parameter. If it fails to convert because of a problem such as input string being in the wrong format, then it doesn't throw an exception. Instead it will return false, and the out parameter will be set to zero. This gives you the benefit of not having to catch an exception in the case of input being in the wrong format.

As Bradley pointed out, if you use int.TryParse() you should always check the return value. If it's true you know your conversion was successful. If it's false you know something went wrong. So in case it's false, before proceeding to next steps in your program you should do something about your input string not being a valid integer.

Sach
  • 10,091
  • 8
  • 47
  • 84
  • of course; using TryParse can just hide the error and make you wonder why nothing is happening... – BradleyDotNET Jul 24 '18 at 20:42
  • @BradleyDotNET well, yes and no. If you use `TryParse()` you _should_ always check the return value. And that will tell you why nothing is happening. – Sach Jul 24 '18 at 20:44
  • Oh I agree, not sure the OP would understand that though :) – BradleyDotNET Jul 24 '18 at 20:45
  • OK I'll add an explanation. – Sach Jul 24 '18 at 20:45
  • txtId.text value stored in database as a form of integer and i want to show it in a textbox. – hod Jul 24 '18 at 20:56
  • I think you must read the .NET documentation for the two methods. You don't need to use the `bool` value, the `out int value` parameter in the example I provided for the `TryPrase()` method will contain the int value if conversion is successful. Please read the answer in full and also run the two code samples I've shown. And read documentation. – Sach Jul 24 '18 at 20:58