Unable to Insert new object: Exception: Primary key is null

Thread is closed for posting
4 posts, 1 answers
  1. Azel
    Azel avatar
    2 posts
    Member since:
    Sep 2009

    Posted 17 Jun 2010 Link to this post

    I had used forward mapping to create my classes. My code are as follow:

                IObjectScope scope = ObjectScopeProvider1.GetNewObjectScope(); 
                ScheduledWorkshop sw = new ScheduledWorkshop(); 
                sw.Id = tbScheduledWorkshopID.Text; 
                sw.Classroom = tbClassroom.Text; 
                sw.Scheduleddatetime = new DateTime(calDate.SelectedDate.Year, calDate.SelectedDate.Month, calDate.SelectedDate.Day, int.Parse(ddlHour.Text), int.Parse(ddlMinutes.Text),0); 
                // The registration datetime is 24 hours before the class commence 
                sw.Registerationenddatetime = sw.Scheduleddatetime.AddHours(-24); 
                sw.Lecturername = tbLecturerName.Text; 
                sw.Maxclasssize = int.Parse(tbMaxClassSize.Text); 
                sw.Status = ddlStatus.Text; 
                sw.Remarks = tbRemarks.Text; 
                sw.Workshopid = ddlWorkshopName.SelectedValue; 
                sw.Lastmodified = DateTime.Now; 
                sw.Lastmodifiedby = "Session User"

    In the MSSQL 2008 Express table, scheduledworkshop's id column is a primary but not auto-incr. The orm mapper's auto-inc is unticked.

    I get an exception @ the scope.transaction.commit(); line that seem to point that my primary key "id" is null. The Sql seems to show that it had not inserted the primary key value that I had assigned.


    Telerik.OpenAccess.Exceptions.DataStoreException was unhandled by user code 
      Message=Insert of '532097884-9' failed: Telerik.OpenAccess.RT.sql.SQLException: Cannot insert the value NULL into column 'id'table 'mdisexperience.dbo.ScheduledWorkshop'column does not allow nulls. INSERT fails. 
    The statement has been terminated. 
       at Telerik.OpenAccess.RT.Adonet2Generic.Impl.PreparedStatementImp.execute() 
       at OpenAccessRuntime.Relational.conn.PooledPreparedStatement.execute() 
       at OpenAccessRuntime.Relational.RelationalStorageManager.generateInserts(NewObjectOID oid, Int32 index, ClassMetaData cmd, PersistGraph graph, Int32[] fieldNos, CharBuf s, Object[] oidData, IntArray toUpdateIndexes) 
    INSERT INTO [ScheduledWorkshop] ([classroom], [lastmodified], [lastmodifiedby], [lecturername], [maxclasssize], [registerationenddatetime], [remarks], [scheduleddatetime], [status], [workshopid]) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) 
    select scope_identity() 
    (set event logging to all to see parameter values) Telerik.OpenAccess.RT.sql.SQLException: Cannot insert the value NULL into column 'id'table 'mdisexperience.dbo.ScheduledWorkshop'column does not allow nulls. INSERT fails. 
    The statement has been terminated. 
       at Telerik.OpenAccess.RT.Adonet2Generic.Impl.PreparedStatementImp.execute() 
       at OpenAccessRuntime.Relational.conn.PooledPreparedStatement.execute() 
       at OpenAccessRuntime.Relational.RelationalStorageManager.generateInserts(NewObjectOID oid, Int32 index, ClassMetaData cmd, PersistGraph graph, Int32[] fieldNos, CharBuf s, Object[] oidData, IntArray toUpdateIndexes) 
           at Telerik.OpenAccess.SPI.Backends.ThrowException(Exception e) 
           at Telerik.OpenAccess.RT.ExceptionWrapper.Throw() 
           at OpenAccessRuntime.DataObjects.OpenAccessPersistenceManagerImp.handleException(Exception x) 
           at OpenAccessRuntime.DataObjects.OpenAccessPersistenceManagerImp.internalCommit(Boolean phase) 
           at OpenAccessRuntime.DataObjects.OpenAccessPersistenceManagerImp.commit() 
           at OpenAccessRuntime.DataObjects.UnsynchronizedPMProxy.commit() 
           at Telerik.OpenAccess.RT.TransactionImpl.Commit() 
           at ExperienceManagementSystem.scheduleworkshop.btnAddWorkshop_Click(Object sender, EventArgs e) in C:\Users\Azel\documents\visual studio 2010\Projects\ExperienceManagementSystem\ExperienceManagementSystem\scheduleworkshop.aspx.cs:line 37 
           at System.Web.UI.WebControls.Button.OnClick(EventArgs e) 
           at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) 
           at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) 
           at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) 
           at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) 
           at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) 
      InnerException: Telerik.OpenAccess.RT.sql.SQLException 
           Message=Cannot insert the value NULL into column 'id'table 'mdisexperience.dbo.ScheduledWorkshop'column does not allow nulls. INSERT fails. 
    The statement has been terminated. 
           Description=SQLState=;Cannot insert the value NULL into column 'id'table 'mdisexperience.dbo.ScheduledWorkshop'column does not allow nulls. INSERT fails. 
    The statement has been terminated. 
                at Telerik.OpenAccess.RT.Adonet2Generic.Impl.PreparedStatementImp.execute() 
                at OpenAccessRuntime.Relational.conn.PooledPreparedStatement.execute() 
                at OpenAccessRuntime.Relational.RelationalStorageManager.generateInserts(NewObjectOID oid, Int32 index, ClassMetaData cmd, PersistGraph graph, Int32[] fieldNos, CharBuf s, Object[] oidData, IntArray toUpdateIndexes) 

    I had recreated all the mappings from scratch a few times using the ORM Wizard but the error still persist. What seems to be the errror here?

  2. Answer
    Serge avatar
    375 posts

    Posted 21 Jun 2010 Link to this post

    Hello Azel Low,

    Actually Telerik OpenAccess ORM provides an easy mechanism for fine-tuning the object identity. You can read more about that in this help article. Basically you have to set the class to have Single Field Identity - choose the column and select NONE for key generator.
    I hope this helps and please contact us back if you need further assistance.

    All the best,
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. Phil
    Phil avatar
    84 posts
    Member since:
    Apr 2005

    Posted 28 Aug 2012 Link to this post

    That link points to "OLD API" help. Is there a link for the new API as that's not much use.
  4. Zoran
    Zoran avatar
    534 posts

    Posted 31 Aug 2012 Link to this post

    Hi Philip,

     Do you face the same issue with the Visual Designer? If yes, then you should just select the entity that has an integer field as the identity member, and you should set its Identity Mechanism to 'Default' in the properties pane. If you want to have the id field automatically calculated by the server (AUTOINC) you should choose the 'DatabaseServerCalculated' value for the Identity Mechanism. Here is an article describing the properties in the Visual Designer properties window.

    Kind regards,
    the Telerik team
    Follow @OpenAccessORM Twitter channel to be the first one to get the latest updates on new releases, tips and tricks and sneak peeks at our product labs!
Back to Top