How to calculate values ββon textchanged event?
I tried on textchanged event
decimal a1 = Convert.ToDecimal(0.00); decimal b1 = Convert.ToDecimal(0.00); decimal c1 = Convert.ToDecimal(0.00); a1 = Convert.ToDecimal(txt_Advance.Text.Trim().ToString()); b1 = Convert.ToDecimal(txtTotal.Text.Trim().ToString()); c1 = b1 - a1; txt_Net.Text = c1.ToString();
This only works once when I change the value again. It gives wrong format error in txt_Advance.text
source to share
You need to use the method TryParse
. What actually happens is that when you enter
value something, it parses correctly. But when you click backspace
, that value is removed and textbox
becomes empty
, so trying to convert empty string into decimal
gives format exception
.
Either you can use the method TryParse
, or you can check that if the value textbox
is equal not empty
to convert
, it matches the value decimal
.
decimal.TryParse(txt_Advance.Text.Trim().ToString(),out a1);
OR
if(!string.IsNullOrEmpty(txt_Advance.Text.Trim().ToString()))
a1 = Convert.ToDecimal(txt_Advance.Text.Trim().ToString());
source to share