base.Save() error messagion: Invalid operation - update not allowed

base.Save() error messagion: Invalid operation - update not allowed

Old forum URL: forums.lhotka.net/forums/t/1967.aspx


shaase posted on Wednesday, December 13, 2006

I am using version 1.53 in conjuction with the CodeSmith 3.2 Editable Child Object template.

When I go to 'save' the contents of my object back to the SQL Server 2000 database, I get the error below.  This object is not part of a collection.  Bascially, it's really simple.  I have attached my code to the end of this post (minus the property definitions).

Is there an easy fix for this error???  I must be missing something here.

Thanx in advance,

SKH

Invalid operation - update not allowed

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NotSupportedException: Invalid operation - update not allowed

Source Error:

Line 399:		//				}
Line 400:		//			}
Line 401:			return base.Save();
Line 402:		}
Line 403:		#endregion

Source File: c:\inetpub\wwwroot\cap_wf_tfs\data objects\contact_single_obj.cs    Line: 401


using System;
using System.Data;
using System.Data.SqlClient;
using CSLA;
using CSLA.Data;

namespace Workflows.dataObjects
{
 /// <summary>
 /// This business object represents the properties and methods of the dbo.Contacts Table.
 /// It corresponds to the EDITABLE CHILD OBJECT as outlined in the Book "Expert One-on-One VB.NET Business Objects"
 /// Object was generated on 9/13/2006 10:01:28 PM - By shaase
 /// </summary>
 /// <remarks>
 /// Parameters used to generate this class.
 /// Business Object
 ///  ClassNamespace      = Workflows.dataObjects
 ///  ObjectName          = Contact_Single_Obj
 ///  RootTable           = PEOGCSWF.dbo.Contacts
 ///
 /// Child Options
 ///  ChildCollectionName =
 ///  ChildMemberName     =
 ///  ParentName          =
 ///  ParentType          =
 ///
 /// Options
 ///  MemberPrefix        = m_
 ///  FLCPrefix           =   
 ///  Accessibility       = Public
 ///  Serializable        = True
 ///  MustBeInherited     = False
 ///  TransactionType     = ADO
 ///
 /// Stored Procedure Style
 ///  GeneralSPPrefix     =
 ///  InsertPrefix        = Add
 ///  UpdatePrefix        = Update
 ///  DeletePrefix        = Delete
 ///  SelectPrefix        = Get
 /// </remarks>
 [Serializable]
 public class Contact_Single_Obj : CSLA.BusinessBase
 {

  #region Class Level Private Variables

  private int m_ID = 0;  //**PK
  private string m_Account = string.Empty;
  private string m_FirstName = string.Empty;
  private string m_MiddleName = string.Empty;
  private string m_LastName = string.Empty;
  private string m_Location = string.Empty;
  private string m_EmailAddress = string.Empty;
  private string m_JobTitle = string.Empty;
  private string m_EmailDisplayName = string.Empty;
  private string m_BusinessPhone = string.Empty;
  private string m_BusinessPhone2 = string.Empty;
  private string m_Company = string.Empty;
  private string m_BusinessStreet = string.Empty;
  private string m_BusinessStreet2 = string.Empty;
  private string m_BusinessStreet3 = string.Empty;
  private string m_BusinessCity = string.Empty;
  private string m_BusinessState = "MI";
  private string m_BusinessPostalCode = string.Empty;
  private string m_BusinessCountry = "USA";
  

  #endregion //Class Level Private Variables

  #region Constructors
  public Contact_Single_Obj()
  {
   // prevent direct creation
   // Show the framework that this is a child object
//   MarkAsChild();
   CheckRules("");
  }
  #endregion //Constructors


  public bool IsSaveable
  {
   //Since you cannot bind a control to multiple properties you need to create a property that combines the ones you need
   //In this case, bind the UI Save button Enabled property to IsSaveable. (Why save an object that has not changed?)
   get { return IsValid && IsDirty; }
  }
  private void CalculateDisplayName()
  {
   m_EmailDisplayName = m_LastName + ", " + m_FirstName + m_MiddleName.Trim() + " (" + this.m_EmailAddress + ")";

  }
  #endregion //Business Properties and Methods


  public override BusinessBase Save()
  {
   return base.Save();
  }
  #endregion

  #region Static Methods
  public static Contact_Single_Obj NewContact(int id)
  {
   return (Contact_Single_Obj) DataPortal.Create(new Criteria(id));
  }

  public static Contact_Single_Obj GetContact(int ID)
  {
   return (Contact_Single_Obj) DataPortal.Fetch(new Criteria(ID));
  }
  #endregion //Static Methods

  #region Criteria (identifies the Individual Object/ Primary Key)
  [Serializable]
   private class Criteria
  {
   
   public int m_id = 0;  //**PK
   public Criteria(int ID)
   {
    this.m_id = ID;
   }
  }

  #endregion //Criteria

  #region Data Access
  protected override void DataPortal_Create(object criteria)
  {
   // Called by DataPortal so that we can set defaults as needed
   // Create new object with default stored in the database
   Criteria crit = (Criteria) criteria;
   m_ID = crit.m_id;
   CheckRules("");
  }
  protected override void DataPortal_Fetch(object criteria)
  {
   //Called by DataPortal to load data from the database
   //retrieve data from database
   Criteria crit = (Criteria) criteria;
   SqlConnection cn = new SqlConnection(DB("CAP_WF"));
   SqlCommand cm = new SqlCommand();

   cn.Open();
   try
   {
    try
    {
     cm.Connection = cn;
     cm.CommandType = CommandType.StoredProcedure;
     cm.CommandText = "contacts_get";
     cm.Parameters.Add("@ID", crit.m_id); // 0 brings back all contacts

     SafeDataReader dr = new SafeDataReader(cm.ExecuteReader());
     try
     {
      while (dr.Read())
      {
       this.m_ID = dr.GetInt32(0);
       this.LastName = dr.GetString(1);
       this.FirstName = dr.GetString(2);
       this.Account = dr.GetString(3);
       this.Location = dr.GetString(4);
       this.EmailAddress = dr.GetString(5);
       this.EmailDisplayName = dr.GetString(6);
       this.BusinessPhone = dr.GetString(7);
       this.BusinessPhone2 = dr.GetString(8);
       this.MiddleName = dr.GetString(9);
       this.JobTitle = dr.GetString(10);
       this.Company = dr.GetString(11);
       this.BusinessStreet = dr.GetString(12);
       this.BusinessStreet2 = dr.GetString(13);
       this.BusinessStreet3 = dr.GetString(14);
       this.BusinessCity = dr.GetString(15);
       this.BusinessState = dr.GetString(16);
       this.BusinessPostalCode = dr.GetString(17);
       this.BusinessCountry = dr.GetString(18);
       MarkOld();
       CheckRules("");
      }
     }
     finally
     {
      dr.Close();
     }
    }
    catch(Exception ex)
    {
     throw(ex);
    }
   }
   finally
   {
    cn.Close();
   }
  }

  internal void Update()
  {
   //Called by DataPortal to delete/add/update data into the database
   // This occurs when you use the collection.save() method
   SqlConnection cn = new SqlConnection(DB("CAP_WF"));
   SqlTransaction tr;
   SqlCommand cm = new SqlCommand();
   cn.Open();
   try
   {
    tr = cn.BeginTransaction(IsolationLevel.Serializable);
    try
    {
     cm.Connection = cn;
     cm.Transaction = tr;
     cm.CommandType = CommandType.StoredProcedure;

     if(this.IsDeleted)
     {
      //is deleted object, check if new
      if(!this.IsNew)
      {
       cm.CommandText = "contacts_delete";
       cm.Parameters.Add("@id", m_ID);
       cm.ExecuteNonQuery();
      }
      // reset the object status to be new
      MarkNew();
     }
     else
     {
      if (this.IsDirty)
      {
       // is not deleted object, check if this is an update or insert
       if(this.IsNew)
       {
        //perform an insert, object has not been persisted
        cm.CommandText = "contacts_insert";
        cm.Parameters.Add("@ID", m_ID);
        cm.Parameters["@ID"].Direction = ParameterDirection.Output;
       }
       else
       {
        //perform an update, object is not new so object has already been persisted
        cm.CommandText = "contacts_update";
        cm.Parameters.Add("@ID", m_ID);
       }
       cm.Parameters.Add("@Account", m_Account);
       cm.Parameters.Add("@FirstName", m_FirstName);
       cm.Parameters.Add("@MiddleName", m_MiddleName);
       cm.Parameters.Add("@LastName", m_LastName);
       cm.Parameters.Add("@Location", m_Location);
       cm.Parameters.Add("@EmailAddress", m_EmailAddress);
       cm.Parameters.Add("@EmailDisplayName", m_EmailDisplayName);
       cm.Parameters.Add("@BusinessPhone", m_BusinessPhone);
       cm.Parameters.Add("@BusinessPhone2", m_BusinessPhone2);
       cm.Parameters.Add("@Company", m_Company);
       cm.Parameters.Add("@BusinessStreet", m_BusinessStreet);
       cm.Parameters.Add("@BusinessStreet2", m_BusinessStreet2);
       cm.Parameters.Add("@BusinessStreet3", m_BusinessStreet3);
       cm.Parameters.Add("@BusinessCity", m_BusinessCity);
       cm.Parameters.Add("@BusinessState", m_BusinessState);
       cm.Parameters.Add("@BusinessPostalCode", m_BusinessPostalCode);
       cm.Parameters.Add("@BusinessCountry", m_BusinessCountry);
       cm.Parameters.Add("@JobTitle", m_JobTitle);
       cm.ExecuteNonQuery();
       this.m_ID = Convert.ToInt32(cm.Parameters["@ID"].Value); //Grab the new ID from the inserted record.
       MarkOld();
      }
     }
     tr.Commit();
    }
    catch(Exception ex)
    {
     tr.Rollback();
     throw(ex);
    }
   }
   finally
   {
    cn.Close();
   }
  }
  #endregion //Data Access

 }
}

 

xal replied on Wednesday, December 13, 2006

The problem is that you generated your BO as if it where a child object.
Then you commented out the MarkAsChild call in the constructor, but the update method is still "Update()". The dataportal is looking for a Dataportal_Update(), but since it's not overriden in your class it's looking for that method and it finds the one in businessbase, which ends up throwing that exception.


I hope that helps!

Andrés

shaase replied on Wednesday, December 13, 2006

Andres,

Thank you!  That did it.  I was looking too much at the detail and not enough at the overall picture.

I changed my "
internal void Update()" to "protected override void DataPortal_Update()" and it works!

I can't thank you enough!

Sometimes, I just have to stand up and take a 5 minute walk and then come back after chilling out.

Have a good one!

SKH

Copyright (c) Marimer LLC