I understand this might be considered a duplicate of this: Parse v. TryParse
But, could anybody tell me why this code threw an exception when Tryparse is not supposed to throw any exceptions at all?
If Int32.TryParse(txtOrdLine.Text.Trim, txtOrdLine.Text) = False Then
The value entered in txtOrdLine was "1-4".
The exception was that it could not convert string "1-4" to type Integer. This is on a code behind for a ASP.Net 4.5 site if that makes a difference.
Any information would be appreciated.
Thanks.
The error is occurring because the automatic conversion the second parameter from String
to Int32
is failing.
VB.Net is effectively re-writing your code as:
Dim temp as Int32
temp = Int32.Parse(txtOrdLine.Text) ' Exception here!
Int32.TryParse(txtOrdLine.Text.Trim, temp)
txtOrderLine.Text = temp.ToString()
You'll need to create a temporary Int32
value and pass that into Int32.TryParse()