c# - Why is nullable decimal crashing my object initializer? -


i have object called project.

in project have property definition this:

public decimal? fytotalcost { get; set; } 

then roll through datareader fill list of objects.

while (dr.read())                 {                     #region fill project object                     try                     {                         projects.add(new project                         { fytotalcost = (dr["fy_total_cost"] != null                                                     && dr["fy_total_cost"].tostring() != string.empty                                                     && dr["fy_total_cost"].tostring() != "") ?                                                     decimal.parse(dr["fy_total_cost"].tostring()) :                                                     (decimal?)null, 

this evaluates false in quick watcy:

dr["fy_total_cost"] != null                                                     && dr["fy_total_cost"].tostring() != string.empty                                                     && dr["fy_total_cost"].tostring() != "" 

but seems getting executed:

decimal.parse(dr["fy_total_cost"].tostring()) 

instead of this:

(decimal?)null 

i'm trace.writing output window:

this get:

a first chance exception of type 'system.formatexception' occurred in  mscorlib.dll  fy:   fy_total_cost:   budget_start_date:   budget_end_date:   error message: system.formatexception: input string not in correct format.  @ system.number.stringtonumber(string str, numberstyles options,   numberbuffer& number, numberformatinfo info, boolean parsedecimal)  @ system.number.parseint32(string s, numberstyles style, numberformatinfo info)  @ system.int32.parse(string s)  @ transferprojects.program.fillprojectsmodel() in c:\development\transferprojects\transferprojects\program.cs:line 172 

because null in database dbnull.value. should checking instead:

fytotalcost = (dr["fy_total_cost"] != dbnull.value ...


Comments

Popular posts from this blog

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - UML - How would you draw a try catch in a sequence diagram? -

c++ - No viable overloaded operator for references a map -