Input string was not in a correct format.

  • Thread starter Thread starter rn5a
  • Start date Start date
R

rn5a

A sub-routine retrieves the value in a TextBox (which ought to be an
integer) which in turn resides in a DataGrid (actually the column is a
BoundColumn which changes to a TextBox when the DataGrid is in the
editable mode). This is the code:

-----------------------------------
Dim strQty As String
Dim iQty As Integer

strQty = CType(ea.Item.Cells(5).Controls(0), TextBox).Text
iQty = CInt(strQty)
-----------------------------------

The last line iQty = CInt(strQty) generates the following error:

Input string was not in a correct format.

What's wrong with the last line in the above code?
 
The problem is here:

strQty = CType(ea.Item.Cells(5).Controls(0), TextBox).Text
iQty = CInt(strQty)

What if the user does not enter a value at all? Or, what if the value
entered is not convertable to an Integer? You need to verify that the data
entered into the textbox is indeed numeric before attempting to convert it
to an Integer.
 
use trace, or some such method, to output the value of "strQty" before the conversion to see what is being returned.
 
Back
Top