You cannot add or change a record because a related record is required in table 'Persons'.

Posts   
 
    
neutcomp
User
Posts: 8
Joined: 16-Nov-2006
# Posted on: 20-Sep-2008 22:53:27   

I have a trange thing. 2 listbox's and 2 buttons to add / remove persons from a team. 1 listbox all persons the other the selected persons.

1 save button with this action behind it: What it does: - reads the dropdownlist witch team is selected - remove the old team members from the selected team - loops trough the selected person listbox and make an generic list - saves the new persons in the selected team


protected void saveButton_Click (object sender, EventArgs e) {
      int teamId = Convert.ToInt32(teamsDropDownList.SelectedValue);

      PersonTeamCollection oPersonTeamCollection = new PersonTeamCollection();
      // Delete the old persons in the selected team
      oPersonTeamCollection.DeleteMulti(PersonTeamFields.TeamId == teamId);

      List<PersonTeamEntity> personList = new List<PersonTeamEntity>();
      foreach (ListItem teamMembers in teamMembersListBox.Items) {
        PersonTeamEntity oPersonTeamEntity = new PersonTeamEntity();
        oPersonTeamEntity.TeamId = teamId;
        oPersonTeamEntity.PersonId = Convert.ToInt32(teamMembers.Value);
        oPersonTeamEntity.CreationDate = DateTime.Now;
        oPersonTeamEntity.UserIdAdded = Convert.ToInt32(ASPInformation.GetUserId());

        personList.Add(oPersonTeamEntity);
      }
      if (personList.Count > 0) {
        PersonTeamCollection oPerson = new PersonTeamCollection(personList);
        oPerson.SaveMulti();
      }
    }

When it goes wrong is: - select a team - select the persons from the listbox - save (fine) - change the dropdown box for a nother team - changes some people (not nessasary) and click save (BOEM!)

The line where it goes wrong is: oPerson.SaveMulti();


/// <summary> Performs the insert action of a new Entity to the persistent storage.</summary>
        /// <returns>true if succeeded, false otherwise</returns>
        protected override bool InsertEntity()
        {
            PersonTeamDAO dao = (PersonTeamDAO)CreateDAOInstance();
            return dao.AddNew(base.Fields, base.Transaction);
        }

And here is the callstack:


[OleDbException (0x80004005): You cannot add or change a record because a related record is required in table 'Persons'.]
   System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult) +267
   System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult) +192
   System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult) +48
   System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method) +106
   System.Data.OleDb.OleDbCommand.ExecuteNonQuery() +108
   SD.LLBLGen.Pro.ORMSupportClasses.ActionQuery.Execute() +556

[ORMQueryExecutionException: An exception was caught during the execution of an action query: You cannot add or change a record because a related record is required in table 'Persons'.. Check InnerException, QueryExecuted and Parameters of this exception to examine the cause of this exception.]
   SD.LLBLGen.Pro.ORMSupportClasses.ActionQuery.Execute() +853
   SD.LLBLGen.Pro.ORMSupportClasses.BatchActionQuery.Execute() +122
   SD.LLBLGen.Pro.ORMSupportClasses.DaoBase.ExecuteActionQuery(IActionQuery queryToExecute, ITransaction containingTransaction) +129
   SD.LLBLGen.Pro.ORMSupportClasses.DaoBase.AddNew(IEntityFields fields, ITransaction containingTransaction) +331
   TheVictoryDall.EntityClasses.PersonTeamEntity.InsertEntity() in D:\My Documents\Visual Studio Projects\TheVictory\TheVictoryDalClass\EntityClasses\PersonTeamEntity.cs:521
   SD.LLBLGen.Pro.ORMSupportClasses.EntityBase.CallInsertEntity() +27
   SD.LLBLGen.Pro.ORMSupportClasses.DaoBase.PersistQueue(List`1 queueToPersist, Boolean insertActions, ITransaction transactionToUse, Int32& totalAmountSaved) +833
   SD.LLBLGen.Pro.ORMSupportClasses.EntityCollectionBase`1.PerformSaveMulti(Boolean recurse) +718
   SD.LLBLGen.Pro.ORMSupportClasses.EntityCollectionBase`1.SaveMulti(Boolean recurse) +149
   SD.LLBLGen.Pro.ORMSupportClasses.EntityCollectionBase`1.SaveMulti() +29
   TheVictory.Website.Admin.Team.TeamLeden.saveButton_Click(Object sender, EventArgs e) in D:\My Documents\Visual Studio Projects\TheVictory\Website\Admin\Team\TeamLeden.aspx.cs:78
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +107
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1746

Some additional information: Selfservicing v2.5 Access DB 2003

I hope you can help me out. Else I have to make a nasty hack by redirecting after the save button.

Thanks Bjorn

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 21-Sep-2008 20:07:33   

Hi Bjorn,

From the error and from your code, I think the issue may be here:

oPersonTeamEntity.PersonId = Convert.ToInt32(teamMembers.Value);

Please debug and double-check that teamMembers.Value indeed contains valid personId's values.

David Elizondo | LLBLGen Support Team