Error on datetime field(s) when calling CustomerService.UpdateCustomer(customer)

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
8 years ago
When calling CustomerService.UpdateCustomer(changedCustomer), nopCommerce v3.30, the following error is thrown;

An error occurred while updating the entries. See the inner exception for details.
System.Data.Entity.Core.UpdateException: An error occurred while updating the entries.
See the inner exception for details. ---> System.Data.SqlClient.SqlException: The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value. The statement has been terminated. at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData() at System.Data.SqlClient.SqlDataReader.get_MetaData() at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds, Boolean describeParameterEncryptionRequest) at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite) at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext,TResult](TTarget target, Func`3 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed) at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.Reader(DbCommand command, DbCommandInterceptionContext interceptionContext) at System.Data.Entity.Core.Mapping.Update.Internal.DynamicUpdateCommand.Execute(Dictionary`2 identifierValues, List`1 generatedValues) at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update() --- End of inner exception stack trace --- at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update() at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess) at System.Data.Entity.Core.Objects.ObjectContext.SaveChangesToStore(SaveOptions options, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction) at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation) at System.Data.Entity.Core.Objects.ObjectContext.SaveChangesInternal(SaveOptions options, Boolean executeInExistingTransaction) at System.Data.Entity.Internal.InternalContext.SaveChanges()


It looks like a new value for one of the datetime fields in [Customer] (columns 'CreatedOnUtc', 'LastLoginDateUtc' and 'LastActivityDateUtc') (or omitting it), causes a value bigger than the datetime type for those columns.
Should these be casted to a smaller value (on update), should the columns be modified to the (larger) datetime2 format, or some other adjustment?
8 years ago
st3v3n wrote:
When calling CustomerService.UpdateCustomer(changedCustomer), nopCommerce v3.30, the following error is thrown;

An error occurred while updating the entries. See the inner exception for details.
System.Data.Entity.Core.UpdateException: An error occurred while updating the entries.
See the inner exception for details. ---> System.Data.SqlClient.SqlException: The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value. The statement has been terminated. at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData() at System.Data.SqlClient.SqlDataReader.get_MetaData() at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds, Boolean describeParameterEncryptionRequest) at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite) at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext,TResult](TTarget target, Func`3 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed) at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.Reader(DbCommand command, DbCommandInterceptionContext interceptionContext) at System.Data.Entity.Core.Mapping.Update.Internal.DynamicUpdateCommand.Execute(Dictionary`2 identifierValues, List`1 generatedValues) at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update() --- End of inner exception stack trace --- at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update() at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess) at System.Data.Entity.Core.Objects.ObjectContext.SaveChangesToStore(SaveOptions options, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction) at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation) at System.Data.Entity.Core.Objects.ObjectContext.SaveChangesInternal(SaveOptions options, Boolean executeInExistingTransaction) at System.Data.Entity.Internal.InternalContext.SaveChanges()


It looks like a new value for one of the datetime fields in [Customer] (columns 'CreatedOnUtc', 'LastLoginDateUtc' and 'LastActivityDateUtc') (or omitting it), causes a value bigger than the datetime type for those columns.
Should these be casted to a smaller value (on update), should the columns be modified to the (larger) datetime2 format, or some other adjustment?


Check you may try to set null in  the any not null datetime column(LastActivityDateUtc or CreatedOnUtc)?
8 years ago
In the database, table [Customer], the datetime columns are;
* CreatedOnUtc - not null
* LastLoginDateUtc - nullable
* LastActivityDateUtc - not null

Items 'CreatedOnUtc' and 'LastActivityDateUtc ' are not null(able), the column 'LastLoginDateUtc' is the only one that can be set to null. But there is no difference between setting the column 'LastLoginDateUtc' to null or to (for example) DateTime.Today. On both values the same error.
8 years ago
st3v3n wrote:
In the database, table [Customer], the datetime columns are;
* CreatedOnUtc - not null
* LastLoginDateUtc - nullable
* LastActivityDateUtc - not null

Items 'CreatedOnUtc' and 'LastActivityDateUtc ' are not null(able), the column 'LastLoginDateUtc' is the only one that can be set to null. But there is no difference between setting the column 'LastLoginDateUtc' to null or to (for example) DateTime.Today. On both values the same error.


how you are trying to save the date can you send a piece of code that assign the date to datetime fields??

i think it should work if you use
customer.CreatedOnUTC=DateTime.UtcNow
8 years ago
st3v3n wrote:
In the database, table [Customer], the datetime columns are;
* CreatedOnUtc - not null
* LastLoginDateUtc - nullable
* LastActivityDateUtc - not null

Items 'CreatedOnUtc' and 'LastActivityDateUtc ' are not null(able), the column 'LastLoginDateUtc' is the only one that can be set to null. But there is no difference between setting the column 'LastLoginDateUtc' to null or to (for example) DateTime.Today. On both values the same error.


Hi,
try like below instead DateTime.Today
Set

customer.CreatedOnUtc= DateTime.UtcNow ;
  customer.LastActivityDateUtc=DateTime.UtcNow ;
8 years ago
Goodmorning Sohel,

Thanks for your support. Setting DateTime.UtcNow didn't solve the problem. I then created some new code (simplified version of the initial code), where the UpdateCustomer worked. Turns out the datetime(2) error occured not on the customer, but on the Nop.Core.Domain.Common.Address I added for/to the Customer. Setting the "CreatedOnUtc"-property for that new Address solved the problem.

I think it would be very logical/efficient if [Address].[CreatedOnUtc] (datetime, not nullable) would have a default value (GETDATE()).
And why make it settable?! (In my opinion all those CreatedOn fields should be readonly)

Anyway, problem solved, thanks again!
8 years ago
st3v3n wrote:
Goodmorning Sohel,

Thanks for your support. Setting DateTime.UtcNow didn't solve the problem. I then created some new code (simplified version of the initial code), where the UpdateCustomer worked. Turns out the datetime(2) error occured not on the customer, but on the Nop.Core.Domain.Common.Address I added for/to the Customer. Setting the "CreatedOnUtc"-property for that new Address solved the problem.

I think it would be very logical/efficient if [Address].[CreatedOnUtc] (datetime, not nullable) would have a default value (GETDATE()).
And why make it settable?! (In my opinion all those CreatedOn fields should be readonly)

Anyway, problem solved, thanks again!


Welcome. And also thanks for informing !!!!!!!!
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.