This is a migrated thread and some comments may be shown as answers.

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

3 Answers 184 Views
Getting Started
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Azel
Top achievements
Rank 1
Azel asked on 17 Jun 2010, 01:18 PM
I had used forward mapping to create my classes. My code are as follow:

            IObjectScope scope = ObjectScopeProvider1.GetNewObjectScope(); 
            scope.Transaction.Begin(); 
            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"
            scope.Add(sw); 
            scope.Transaction.Commit(); 

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.

Exception:

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) 
  Source=Telerik.OpenAccess 
  CanRetry=false 
  StackTrace: 
       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. 
       Source=Telerik.OpenAccess.Adonet2 
       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. 
       ErrorCode=515 
       Number=515 
       StackTrace: 
            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) 
       InnerException:  
 

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?

3 Answers, 1 is accepted

Sort by
0
Accepted
Serge
Telerik team
answered on 21 Jun 2010, 07:29 PM
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,
Serge
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
0
Phil
Top achievements
Rank 2
answered on 28 Aug 2012, 01:27 PM
That link points to "OLD API" help. Is there a link for the new API as that's not much use.
0
Zoran
Telerik team
answered on 31 Aug 2012, 08:31 AM
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,
Zoran
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!
Tags
Getting Started
Asked by
Azel
Top achievements
Rank 1
Answers by
Serge
Telerik team
Phil
Top achievements
Rank 2
Zoran
Telerik team
Share this question
or