Reputation: 3575
Hello I might have strings for example "1,1"
or "1.1"
or "1"
or ""
and from each of them I need to get results in same format so it can be saved in SQL Db as numeric (8,2)
var ci = CultureInfo.InvariantCulture.Clone() as CultureInfo;
prikaz.Parameters.AddWithValue("@p_ub_c", Decimal.Parse(p_ub_c.Text.Replace(',', '.') == string.Empty ? "0.00" : p_ub_c.Text, ci));
Passed string is: "2500,00"
but can be also ""
or "2500.00"
Those values needs to be saved in sql numeric(8,2)
And when the textbox p_ub_cis
empty then string is 0 and I got this: System.FormatException input string was not in correct format.
May someone help me solve this out?
Thank you for your time
Upvotes: 0
Views: 147
Reputation: 7009
You never use what Replace
returns, you should use it as following:
string.IsNullOrEmpty(p_ub_c.Text) ? "0.00" : p_ub_c.Text.Replace(',', '.');
Upvotes: 2
Reputation: 43264
Your problem lies with the follow code fragment:
p_ub_c.Text.Replace(',', '.') == string.Empty ? "0.00" : p_ub_c.Text
This says 'take my Text and replace "," with ".", then if the result is an empty string, use "0.00", otherwise use Text'. In other words, you lose the result of the Replace.
If you change it to store the results of the Replace and parse that, it should fix the problem. Beware of someone entering eg "1,000.1" though as you'll end up with "1.000.1", which is still an invalid number.
Upvotes: 2