- Home
- LLBLGen Pro
- Bugs & Issues
Cannot insert the value NULL into column
Joined: 14-Mar-2007
Hi,
I'm trying to solve/reproduce a very annoying problem which is occurring at one of our clients. I can't reproduce the problem in our test environment and the problem isn't occuring on a constant basis. The "SessionId" field in the HeaderEntity is a foreign key of the Session table. In most cases the save action is successful but sometimes the "SessionId" becomes NULL while saving. I'm pretty much convinced (99%) the column wasn't null when calling the save on the adapter.
My question is: Is it possible that LLBL thinks the SessionId is null but actually isn't? Or could it be that LLBL sets the column to null depending on certain circumstances?
Kind regards,
Andre
- LLBL version 2.0.0.0 Final
- LLBL runtime version v2.0.50727
- .NET version: VS2008 - 9.0.21022.8 Adapter model.
Stacktrace:
[2009-05-23 03:37:28.202] [Error] [1] SaveEntity failed. Entity ORMapper.EntityClasses.HeaderEntity, Exception SD.LLBLGen.Pro.ORMSupportClasses.ORMQueryExecutionException: An exception was caught during the execution of an action query: Cannot insert the value NULL into column 'SessionId', table 'PosTransactions7073.dbo.Header'; column does not allow nulls. INSERT fails. The statement has been terminated.. Check InnerException, QueryExecuted and Parameters of this exception to examine the cause of this exception. ---> System.Data.SqlClient.SqlException: Cannot insert the value NULL into column 'SessionId', table 'PosTransactions7073.dbo.Header'; column does not allow nulls. INSERT fails. The statement has been terminated. at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) 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) at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at SD.LLBLGen.Pro.ORMSupportClasses.ActionQuery.Execute() --- End of inner exception stack trace --- at SD.LLBLGen.Pro.ORMSupportClasses.ActionQuery.Execute() at SD.LLBLGen.Pro.ORMSupportClasses.BatchActionQuery.Execute() at SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase.ExecuteActionQuery(IActionQuery queryToExecute) at SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase.PersistQueue(List`1 queueToPersist, Boolean insertActions) at SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase.SaveEntity(IEntity2 entityToSave, Boolean refetchAfterSave, IPredicateExpression updateRestriction, Boolean recurse) at DataAccess.Common.CustomDataAccessAdapter.SaveEntity(IEntity2 entityToSave, Boolean refetchAfterSave, IPredicateExpression updateRestriction, Boolean recurse) at DataAccess.Common.Manager.SaveEntity(CallContext context, EntityBase2 entity, Boolean recurse, String identifier), InnerException System.Data.SqlClient.SqlException: Cannot insert the value NULL into column 'SessionId', table 'PosTransactions7073.dbo.Header'; column does not allow nulls. INSERT fails. The statement has been terminated. at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) 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) at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at SD.LLBLGen.Pro.ORMSupportClasses.ActionQuery.Execute()
CowHills wrote:
My question is: Is it possible that LLBL thinks the SessionId is null but actually isn't? Or could it be that LLBL sets the column to null depending on certain circumstances?
No, AFAIK. Must be something about the circumstances your app perform the save (concurrency? some GUI bug? some business logic oversight?).
Joined: 13-Apr-2012
daelmo wrote:
CowHills wrote:
My question is: Is it possible that LLBL thinks the SessionId is null but actually isn't? Or could it be that LLBL sets the column to null depending on certain circumstances?
No, AFAIK. Must be something about the circumstances your app perform the save (concurrency? some GUI bug? some business logic oversight?).
I've run across this as well today. I had a boolean on an entity that if not explicitly set before saving would through this error "...Cannot insert the value NULL into column 'X'..."
I looked into the inner exception like it said, and what I found it was creating an INSERT statement that didn't include property X which is not a null-able collumn.
Assumptions begin: The save method only saves IsChanged properties, and the 'X' property, when not explicitly set is not marked as changed and wasn't included in the INSERT statement? This led to the attempted NULL value in the column as the column default is not set. This seems to be in direct conflict with the entity being new. .NET defaults and SQL Server defaults aren't the same and can't be assumed.
private void ShowAddOrderItemDialog(object sender, EventArgs e)
{
var newOrderItem = new LabsEntity();
newOrderItem.InitializeEntity();
newOrderItem.SortIndex = 0;
newOrderItem.Active = true;
//Guessing the save is based on if the properties are changed, which seems to be incompatible with a new instantiation.
//Alas, if these two aren't set directly, they will not be sent in the insert statement and will not be saved. -Jack
//newOrderItem.RequiresIndication = false;
//newOrderItem.RequiresAdministerTime = false;
ShowItemDialog(newOrderItem, Add, Cancel);
}
For now we just ensure everything that the save complains can't be null is initialized in a builder on instantiation.
Any insight into this in the past few years?
- SQL Server 10.50.1600
- LLBLGen Pro version: 2.6
- Runtime lib BUILD: Unknown at the moment (I don't regen)
- .NET Framework 3.5
Templates:SD.TemplateBindings.SqlServerSpecific.NET20 Templates vendor: Solutions Design.
Jackson wrote:
I've run across this as well today. I had a boolean on an entity that if not explicitly set before saving would through this error "...Cannot insert the value NULL into column 'X'..."
I looked into the inner exception like it said, and what I found it was creating an INSERT statement that didn't include property X which is not a null-able collumn.
Assumptions begin: The save method only saves IsChanged properties, and the 'X' property, when not explicitly set is not marked as changed and wasn't included in the INSERT statement? This led to the attempted NULL value in the column as the column default is not set. This seems to be in direct conflict with the entity being new. .NET defaults and SQL Server defaults aren't the same and can't be assumed.
You are right, the values has to be set, unless you set default values at DB side. This is the way it should work. If you want defaults at .Net side, then you should override one of the Initialization methods in your entity to set a default value of your fields, so the field is always set. This is explained in the docs: Entities, NULL values and Defaults
Jackson wrote:
For now we just ensure everything that the save complains can't be null is initialized in a builder on instantiation.
![]()
That is the way to do it
Jackson wrote:
Any insight into this in the past few years?
No, as this is the way it should work, LLBLGen can't assume default values for you. If you don't set something is because you don't want it to be set, so it's not taken into account in inserts/updates, otherwise. If you want defaults either set them at DB or when the entity is initialized.
btw, please don't resurrect old threads in the future, this is why. Thanks