c# - Error when trying to convert TextBox.Text value to Int32 -
in txtdefinavalor.text
can enter values 1% 999%, made code if value higher 100%, change value of textbox 100%. it, removed % textbox , made if code using convert.int32 (had same error), tried int32.parse (had same error).
error "input string not in incorrect format" on line:
string valor = txtdefinavalor.text.replace("%", string.empty);
i think textbox after replace % "" integer number , wasnt need convert, can't make string > value
see full code:
if (rbtpercentualmensal.checked || rbtpercentualmedioanual.checked) { string valor = txtdefinavalor.text.replace("%", string.empty); if (int32.parse(valor) > 100) { txtdefinavalor.text = "100%"; } }
how solve it?
try replaceing "." , "," (if users entered them), , trim!
string valor = txtdefinavalor.text.replace("%", string.empty).replace(",", string.empty).replace(".", string.empty).trim();
or
there may invalid int char in string. solve this, use foreach loop:
foreach (char letter in valor) { if(!char.isdigit(letter)) valor = valor.replace(letter.tostring(), string.empty); }
Comments
Post a Comment