c# - cmd.Parameters.AddWithValue nullable int -


in data layer class, initialize parameter so:

private int? _dependencyid;  public int? dependencyid { {return _dependencyid;} set {_dependencyid = value;} }  public constructormethod() {     _dependencyid = (int?)null; } 

in class insert() method, attempting

cmd.addwithvalue("@dependencyid", _dependencyid); 

if _dependencyid has value, well. if _dependencyid null, error:

the parameterized query '(@param1(nvarchar(10), @param2(nvarchar(255), expects parameter '@dependecyid", not supplied

i found [article][1], tried adjusting code so:

cmd.addwithvalue("@dependencyid", _dependencyid == null? dbnull.value : _dependencyid);                              , cmd.addwithvalue("@dependencyid", _dependencyid == null? (int?) dbnull.value : _dependencyid);  

either way, there issues. how should handle this?

thanks in advance help

you need add object:

cmd.addwithvalue("@dependencyid", _dependencyid == null? dbnull.value : (object)_dependencyid);  

you can shorten to

cmd.addwithvalue("@dependencyid", (object)_dependencyid ?? dbnull.value);  

Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -