c# - How to set DateTime object to null and sent as parameter to stored procedure? -
i call stored procedure visual studio project , passing datetime parameter.
here c# code:
datetime? dtstartfrom = null; var result = _context.database.sqlquery<damageeventsdtl>("spdamageeventsdtl @dtstartfrom", new sqlparameter("dtstartfrom", dtstartfrom)).tolist();
here how variable defined in stored procedure:
@dtstartfrom date
on row exception:
the parameterized query '(@dtstartfrom nvarchar(4000))spdamageeventsdtl @dtstartfrom' expects parameter '@dtstartfrom', not supplied.
so guess there problem null values, tryed way:
datetime? dtstartfrom = dbnull.value;
but on row above error:
cannot implicitly convert type 'system.dbnull' 'system.datetime?'
any idea how can set datetime object null , sent parameter stored procedure?
your parameter name should match of query:
new sqlparameter("@dtstartfrom", dtstartfrom);
edit:
datetime? dtstartfrom = null; sqlparameter sqldateparam = new sqlparameter("@dtstartfrom", sqldbtype.datetime); sqldateparam.isnullable = true; if (dtstartfrom.hasvalue) sqldateparam.value = dtstartfrom.value; else sqldateparam.value = dbnull.value var result = _context.database.sqlquery<damageeventsdtl>("spdamageeventsdtl @dtstartfrom", sqldateparam).tolist();
Comments
Post a Comment