asp.net - VB.Net IIf function in adding SqlParameter -


i rewriting long insert statement , parameters used this:

 cmd.parameters.addwithvalue("@website", general.fnsqlnullvalues(twebsite.text)) 

where general.fnsqlnullvalues this:

 public shared function fnsqlnullvalues(byval svalue object, optional byval len integer = 999999) object if svalue nothing return dbnull.value fnsqlnullvalues = iif(svalue.tostring.length = 0, dbnull.value, left(svalue.tostring(), len)) end function 

i don't @ , , seems alot of code this,

 cmd.parameters.addwithvalue("@website" , if(twebsite.text , dbnull.value)) 

from understanding , 1 line of code there dbnull.value replace twebsite.text value if twebsite.text null or not accepted , seems me same thing other function in general. correct , 1 way better other?

also , warning : "cannot infer common type; object assumed" second way , seems me first way using generic object anyways , not know if should ignore warning

strings little tricky when working database parameters in code. if have empty string, pass empty string value insertion. however, if set set string value of nothing, insert null value database. otherwise set empty string.

ideally, you'd have kind of business layer on top of data layer check twebsite.text value , either pass in nothing or text value function sets parameters. different first code example above. way data layer has execute following command:

cmd.parameters.addwithvalue("@website" , valuewebsite)) 

...and no hassle necessary.

for other data types (such integers, decimals, etc.), check out nullable types , work them. make things easy work with. default initialize nothing, if don't assign value (from input boxes) value stays nothing , proper null insert database.


Comments

Popular posts from this blog

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

php - Get uncommon values from two or more arrays -

Adding duplicate array rows in Php -