Thursday, 15 August 2013

c# - VarChar conversion to Decimal -



c# - VarChar conversion to Decimal -

i have records in database next format, eg: 04567.123 datatype varchar. when access them c# app need sum number lastly three, "04567.124" having troubles this. utilize next code seek convert value , +1

using (sqldatareader read1 = cmd1.executereader()) { while (read1.read()) { string num1 = (read1["numerodossier"].tostring()); decimal ndos = convert.todecimal(num1 + 1); textbox10.text = ndos.tostring(); } }

but gives me error, format error , can't figure out :\ can helo me? many thanks!

edit1:

string num1 = (read1["numerodossier"].tostring()); decimal ndos = convert.todecimal(num1, cultureinfo.invariantculture); decimal total = ndos += (0.001); textbox10.text = total.tostring();

solution (many habib):

using (sqldatareader read1 = cmd1.executereader()) { while (read1.read()) { string num1 = (read1["numerodossier"].tostring()); decimal ndos = convert.todecimal(num1, cultureinfo.invariantculture); decimal total = ndos += 0.001m; textbox10.text = total.tostring(); } }

the problem line:

decimal ndos = convert.todecimal(num1 + 1);

assuming num1 set "04567.123" adding 1 homecoming string "04567.1231", not increment lastly digit assumed.

still shouldn't cause conversion error due format. i believe on civilization uses different decimal separator .. should do:

decimal ndos = convert.todecimal(num1, cultureinfo.invariantculture);

and don't need 1 added number need 0.001m

ndos += 0.001m; //m decimal

you may consider decimal.tryparse method.

c# sql winforms

No comments:

Post a Comment