c# - Can't update a record that has a null date value in Telerik Open Access -
i have solution telerik data access project. encountered problem when trying update record. think problem has fact date field null value in database before trying update it, i'm not sure. can tell me how work around it?
in mvc project, have model called tblcolocation. linked our sql database. mode defined in data access project as
namespace project.openaccess { public class tblcolocation { public int locid { get; set; } ... public datetime modon { get; set; } public int modby { get; set; } } }
to update data, in file called repository.cs, have method:
public void updatecolocation(tblcolocation obj) { var upd = dat.tblcolocations.firstordefault(itm => itm.locid == obj.locid); if (upd != null) { upd.modon = obj.modon; upd.modby = obj.modby; } dat.savechanges(); }
i have similar code updating other models, tblcolocation throws error. field modon null in database (this design of our client, null means record hasn't been modified, , want keep data way). we're trying set modon today's date , i've verified model have value modon when hits savechanges() command. if manually put value in modby in database, update works expected. why think having modon null may problem.
the error occurs when gets section (i've stepped through code, , error comes dat.savechanges) this:
server error in '/' application. sqldatetime overflow. must between 1/1/1753 12:00:00 , 12/31/9999 11:59:59 pm.
exception details: system.data.sqltypes.sqltypeexception: sqldatetime overflow. must between 1/1/1753 12:00:00 , 12/31/9999 11:59:59 pm.
[sqltypeexception: sqldatetime overflow. must between 1/1/1753 12:00:00 , 12/31/9999 11:59:59 pm.]
telerik.openaccess.rt.adonet2generic.impl.preparedstatementimp.executeupdate(nullable 1 commandtimeout) +785
openaccessruntime.relational.conn.pooledpreparedstatement.executeupdate(nullable`1 commandtimeout) +146
openaccessruntime.relational.relationalstoragemanager.generateupdates(oid oid, int32 index, classmetadata cmd, persistgraph graph, int32[] fieldnos, boolean havenewobjects, charbuf s, batchcontrolinfo batchcontrol, boolean previousinserts) +3385[datastoreexception: update failed: system.data.sqltypes.sqltypeexception: sqldatetime overflow. must between 1/1/1753 12:00:00 , 12/31/9999 11:59:59 pm. @ telerik.openaccess.rt.adonet2generic.impl.preparedstatementimp.executeupdate(nullable 1 commandtimeout) @ openaccessruntime.relational.conn.pooledpreparedstatement.executeupdate(nullable1 commandtimeout) @ openaccessruntime.relational.relationalstoragemanager.generateupdates(oid oid, int32 index, classmetadata cmd, persistgraph graph, int32[] fieldnos, boolean havenewobjects, charbuf s, batchcontrolinfo batchcontrol, boolean previousinserts) row: genericoid@91ae60ab tblcolocation locid=5 update [tblcolocation] set [address2] = ?, [country] = ?, [modby] = ?, [modon] = ? [locid] = ? , [address2] = ? , [country] = ? , [modby] = ? , [modon] = ? (set event logging see parameter values) system.data.sqltypes.sqltypeexception: sqldatetime overflow. must between 1/1/1753 12:00:00 , 12/31/9999 11:59:59 pm. at
does correct in guessing it's initial null value of modon in database causing problem? if so, how can save record without changing initial value of modon?
this means never set value modon
means it's value 1/1/0001
. value can not stored in sql datetime field. can either make sure obj.modon
has valid date greater 1/1/1753, or change sql column datetime2
type.
you can change value sqldatetime.minvalue before saving. since did not make column optional using datetime?
may have validation issue somewhere.
public void updatecolocation(tblcolocation obj) { var upd = dat.tblcolocations.firstordefault(itm => itm.locid == obj.locid); if (upd != null) { upd.modon = obj.modon < system.data.sqltypes.sqldatetime.minvalue ? system.data.sqltypes.sqldatetime.minvalue : obj.modon; upd.modby = obj.modby; } dat.savechanges(); }
Comments
Post a Comment