sql server - Inserting NULL integer using VB.Net and EF5 -


working on application relies on older version of entity, , i'm trying insert null int field. field in sql server (int, null).

here's definition of object in ef:

<edmscalarpropertyattribute(entitykeyproperty:=false, isnullable:=true)> <datamemberattribute()> public property application_id() nullable(of global.system.int32) 

...and here i'm trying set it:

applications.application_id = iif(isnumeric(txtappid.text), cint(txtappid.text), nothing) 

the error thrown in response is:

an exception of type 'system.invalidcastexception' occurred in ... not handled in user code

additional information: specified cast not valid.

i can confirm issue being thrown due nothing portion because applications.application_id = cint(txtappid.text) , fine.

i've tried dbnull.value instead of nothing, though error reads same. done fair bit of research though issues relate es6 or datetime fields, , such felt issue specific enough warrant own question.

thanks.

the iif function doesn't short circuit, , therefore evaluates both true , false parts, it's not going work in situation. if keyword short circuit, run issues return type , nullable value types (e.g. dim x integer? = if(false, 1, nothing) results in x = 0 because if returning integer , not integer?).

so, recommend either using regular if statement:

if isnumeric(txtappid.text)     applications.application_id = cint(txtappid.text) else     applications.application_id = nothing end if 

or create helper function:

function nullablecint(value string) integer?     if isnumeric(value) return cint(value)     return nothing end function 

and use that:

applications.application_id = nullablecint(txtappid.text) 

Comments

Popular posts from this blog

inversion of control - Autofac named registration constructor injection -

verilog - Systemverilog dynamic casting issues -

ios - Change Storyboard View using Seague -