.net - Dealing with potentially null datetime value in a SQLParameter -


i trying thought pretty straightforward thing, having issue. have code in .net application insert new record table using sqlcommand.

i passing in either datetime value or null. code below doesn't seem work though:

command.parameters.addwithvalue("@settledate",  iif(string.isnullorempty(settledate), nothing, datetime.parse(settledate))) 

where "settledate" string containing date null. think code cover condition of not attempting parse null value, when ran following error looks datetime.parse method:

string reference not set instance of string. parameter name: s 

how be? if "settledate" null should never reach parse method.

you should use short circuit opertator of vb.net dbnull.value

if(string.isnullorempty(settledate), dbnull.value, datetime.parse(settledate))) 

the if operator, contrary iif function doesn't try evaluate both sides of expression if first 1 true. note pass null value sqlparameter use dbnull.value.


Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -