Cannot insert the value NULL into column

Posts   
 
    
CowHills
User
Posts: 47
Joined: 14-Mar-2007
# Posted on: 12-Jun-2009 11:51:57   

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()

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 13-Jun-2009 04:41:41   

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?).

David Elizondo | LLBLGen Support Team
CowHills
User
Posts: 47
Joined: 14-Mar-2007
# Posted on: 13-Jun-2009 21:52:51   

It probably is but so far I haven't been able to reproduce the problem.

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 15-Jun-2009 09:34:39   

I'll close this thread for now, and if you could reproduce it at any time and you think it has something to do with llblgen pro, just reply to this thread and it will be re-opened.

Jackson
User
Posts: 1
Joined: 13-Apr-2012
# Posted on: 13-Apr-2012 20:20:02   

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. confused

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.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 14-Apr-2012 03:08:43   

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. confused

That is the way to do it simple_smile

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 wink

David Elizondo | LLBLGen Support Team