// =====================================================================================
        /// <summary>
        /// This class updates the fields on formfield table using field list, RecordUid and usercommon name.
        /// </summary>
        /// <param name="FormRecord">EvForm object</param>
        /// <returns>EvEventCodes: an event code for updating fields</returns>
        /// <remarks>
        /// This method consists of the following steps:
        ///
        /// 1. Iterate through the formfields object.
        ///
        /// 2. If Guid is empty, add new field.
        ///
        /// 3. Return the event code for updating items.
        /// </remarks>
        // -------------------------------------------------------------------------------------
        public EvEventCodes UpdateFields(
            EdRecord FormRecord)
        {
            this.LogMethod("UpdateFields method ");
            this.LogDebug("RecordFieldList.Count: " + FormRecord.Fields.Count);
            this.LogDebug("SubmitRecord: " + FormRecord.State);
            //
            // Initialize the method debug log and the return event code.
            //
            List <SqlParameter> ParmList           = new List <SqlParameter> ( );
            StringBuilder       SqlUpdateStatement = new StringBuilder( );

            //
            // Define the record Guid value for the update queies
            //
            SqlParameter prm = new SqlParameter(EdRecordValues.PARM_RECORD_GUID, SqlDbType.UniqueIdentifier);

            prm.Value = FormRecord.Guid;
            ParmList.Add(prm);

            //
            // Delete the sections
            //
            SqlUpdateStatement.AppendLine("DELETE FROM ED_RECORD_VALUES "
                                          + "WHERE " + EdRecordValues.DB_RECORD_GUID + "= " + EdRecordValues.PARM_RECORD_GUID + ";  \r\n\r\n");

            //
            // Iterate through the formfields object.
            //
            foreach (EdRecordField field in FormRecord.Fields)
            {
                if (field == null)
                {
                    //this.LogDebugValue ( "FIELD NULL" );
                    continue;
                }

                //
                // If the field guid is empty create a new one.
                //
                if (field.Guid == Guid.Empty)
                {
                    field.Guid = Guid.NewGuid( );
                }
                this.LogDebug("field.FormFieldGuid: " + field.FieldGuid);
                this.LogDebug("field.Guid: " + field.Guid);

                //
                // Create the list of update queries and parameters.
                //
                this.GenerateUpdateQueryStatements(
                    SqlUpdateStatement,
                    ParmList,
                    field);

                this._ValueCount++;
            }//END FormField Update Iteration.

            this.LogDebug(SqlUpdateStatement.ToString( ));

            //
            // Convert the list to an array of SqlPararmeters.
            //
            SqlParameter [] parms = new SqlParameter [ParmList.Count];

            for (int i = 0; i < ParmList.Count; i++)
            {
                parms [i] = ParmList [i];
            }

            this.LogDebug(EvSqlMethods.getParameterSqlText(parms));

            //
            // Execute the update command.
            //
            try
            {
                if (EvSqlMethods.QueryUpdate(SqlUpdateStatement.ToString( ), parms) == 0)
                {
                    return(EvEventCodes.Database_Record_Update_Error);
                }
            }
            catch (Exception Ex)
            {
                this.LogDebug(Evado.Model.EvStatics.getException(Ex));
            }

            return(EvEventCodes.Ok);
        }//END UpdateFields method
    }//END getList method

    #endregion

    #region Get Organisation object section

    // =====================================================================================
    /// <summary>
    /// This class retrieves an organization data table based on Organization Guid
    /// </summary>
    /// <param name="OrgGuid">Guid: Organisation object global unique identifier</param>
    /// <returns>EvOrganisation: an organisation data object</returns>
    /// <remarks>
    /// This method consists of the following steps: 
    /// 
    /// 1. Return an empty Organization object if the Guid is empty. 
    /// 
    /// 2. Define the sql query parameters and sql query string 
    /// 
    /// 3. Execute the sql query string and store the results on the datatable. 
    /// 
    /// 4. Extract the first row data to the organization object. 
    /// 
    /// 5. Return the organization data object. 
    /// </remarks>
    // -------------------------------------------------------------------------------------
    public EdOrganisation getItem ( Guid OrgGuid )
    {
      //
      // Initialize the method status and a return organization object. 
      //
      this.LogMethod ( "getItem method" );
      this.LogDebug ( "OrgGuid: " + OrgGuid );

      EdOrganisation organisation = new EdOrganisation ( );

      // 
      // Validate whether the Guid is not empty.
      // 
      if ( OrgGuid == Guid.Empty )
      {
        this.LogDebug ( "OrgGuid empty" );
        this.LogMethodEnd ( "getItem" );
        return organisation;
      }

      // 
      // Define the SQL query parameters and load the query values.
      // 
      SqlParameter [ ] cmdParms = new SqlParameter [ ] 
      {
        new SqlParameter (  EdOrganisations.PARM_Guid, SqlDbType.UniqueIdentifier )
      };
      cmdParms [ 0 ].Value = OrgGuid;

      // 
      // Construct the Sql query string.
      // 
      sqlQueryString = SQL_SELECT_QUERY
        + "WHERE (" + EdOrganisations.DB_GUID + " = " + EdOrganisations.PARM_Guid + ") ; ";

      this.LogDebug ( sqlQueryString );
      this.LogDebug ( EvSqlMethods.getParameterSqlText ( cmdParms ) );

      // 
      // Execute the query against the database
      //
      using ( DataTable table = EvSqlMethods.RunQuery ( sqlQueryString, cmdParms ) )
      {
        // 
        // If not rows the return
        // 
        if ( table.Rows.Count == 0 )
        {
          this.LogDebug ( "ROW NOT FOUND" );
          this.LogMethodEnd ( "getItem" );

          return organisation;
        }

        // 
        // Extract the table row
        // 
        DataRow row = table.Rows [ 0 ];

        // 
        // Fill the role object.
        // 
        organisation = this.readQueryRow ( row );

        //
        // if the use is not an Evado user and the organisation type is Evado 
        // return an empty organisation object.
        //
        if ( organisation.OrgType == "Evado"
          && this.ClassParameters.UserProfile.hasEvadoAccess == false )
        {
          organisation = new EdOrganisation ( );
        }

      }//END Using 

      // 
      // Return Organisation.
      // 
      this.LogMethodEnd ( "getItem" );
      return organisation;

    }//END getItem method
        }//END GetView method.

        #endregion

        #region FormFields Update queries

        // ==================================================================================
        /// <summary>
        /// This class update items on EV_FORM_SECTION table using retrieving form section items values.
        /// </summary>
        /// <param name="Form">EvForm: a form section object</param>
        /// <returns>EvEventCodes: an event code for updating items on formfield object</returns>
        /// <remarks>
        /// This method consists of the following steps:
        ///
        /// 1. Dekete the data for the old Guid.
        ///
        /// 2. Insert the modified data for the new Guid.
        /// </remarks>
        // ----------------------------------------------------------------------------------
        public EvEventCodes UpdateItem(EdRecord Form)
        {
            this.LogMethod("updateItem method. ");
            this.LogDebug("Section Count: " + Form.Design.FormSections.Count);

            //
            // Initialize the debug status and the local variables
            //
            StringBuilder       sbSQL_AddQuery = new StringBuilder( );
            List <SqlParameter> parmList       = new List <SqlParameter> ( );

            if (Form.Guid == Guid.Empty)
            {
                return(EvEventCodes.Identifier_Global_Unique_Identifier_Error);
            }

            //
            // if sections doe not exist exit.
            //
            if (Form.Design.FormSections.Count == 0)
            {
                return(EvEventCodes.Ok);
            }

            //
            // Delete the sections
            //
            sbSQL_AddQuery.AppendLine("DELETE FROM ED_RECORD_SECTIONS "
                                      + "WHERE " + EdRecordSections.DB_LAYOUT_GUID + " = " + EdRecordSections.PARM_FORM_GUID + ";  \r\n");

            SqlParameter prm = new SqlParameter(EdRecordSections.PARM_FORM_GUID, SqlDbType.UniqueIdentifier);

            prm.Value = Form.Guid;
            parmList.Add(prm);

            for (int count = 0; count < Form.Design.FormSections.Count; count++)
            {
                EdRecordSection section = Form.Design.FormSections [count];

                //
                // skip empty sections
                //
                if (section.Title == String.Empty)
                {
                    continue;
                }

                //
                // Set the order if 0
                //
                if (section.Order == 0)
                {
                    section.Order = count + 3;
                }

                //
                // define the section parameters.
                //
                prm       = new SqlParameter(EdRecordSections.PARM_NUMBER + "_" + count, SqlDbType.Int);
                prm.Value = section.No;
                parmList.Add(prm);

                prm       = new SqlParameter(EdRecordSections.PARM_ORDER + "_" + count, SqlDbType.Int);
                prm.Value = section.Order;
                parmList.Add(prm);

                prm       = new SqlParameter(EdRecordSections.PARM_NAME + "_" + count, SqlDbType.VarChar, 30);
                prm.Value = section.Title;
                parmList.Add(prm);

                prm       = new SqlParameter(EdRecordSections.PARM_INSTRUCTIONS + "_" + count, SqlDbType.NText);
                prm.Value = section.Instructions;
                parmList.Add(prm);

                prm       = new SqlParameter(EdRecordSections.PARM_FIELD_NAME + "_" + count, SqlDbType.NVarChar, 20);
                prm.Value = section.FieldId;
                parmList.Add(prm);

                prm       = new SqlParameter(EdRecordSections.PARM_FIELD_VALUE + "_" + count, SqlDbType.NVarChar, 50);
                prm.Value = section.FieldValue;
                parmList.Add(prm);

                prm       = new SqlParameter(EdRecordSections.PARM_ON_MATCH_VISIBLE + "_" + count, SqlDbType.Bit);
                prm.Value = section.OnMatchVisible;
                parmList.Add(prm);

                prm       = new SqlParameter(EdRecordSections.PARM_VISIBLE + "_" + count, SqlDbType.Bit);
                prm.Value = section.OnOpenVisible;
                parmList.Add(prm);

                prm       = new SqlParameter(EdRecordSections.PARM_DEFAULT_DISPLAY_ROLES + "_" + count, SqlDbType.NVarChar, 250);
                prm.Value = section.ReadAccessRoles;
                parmList.Add(prm);

                prm       = new SqlParameter(EdRecordSections.PARM_DEFAULT_EDIT_ROLES + "_" + count, SqlDbType.NVarChar, 250);
                prm.Value = section.EditAccessRoles;
                parmList.Add(prm);

                prm       = new SqlParameter(EdRecordSections.PARM_PERCENT_WIDTH + "_" + count, SqlDbType.Int);
                prm.Value = section.EditAccessRoles;
                parmList.Add(prm);

                //
                // Create the add query .
                //
                sbSQL_AddQuery.AppendLine(" INSERT INTO ED_RECORD_SECTIONS  "
                                          + "(" + EdRecordSections.DB_LAYOUT_GUID
                                          + ", " + EdRecordSections.DB_NUMBER
                                          + ", " + EdRecordSections.DB_ORDER
                                          + ", " + EdRecordSections.DB_NAME
                                          + ", " + EdRecordSections.DB_INSTRUCTIONS
                                          + ", " + EdRecordSections.DB_FIELD_NAME
                                          + ", " + EdRecordSections.DB_FIELD_VALUE
                                          + ", " + EdRecordSections.DB_ON_MATCH_VISIBLE
                                          + ", " + EdRecordSections.DB_VISIBLE
                                          + ", " + EdRecordSections.DB_DEFAULT_DISPLAY_ROLES
                                          + ", " + EdRecordSections.DB_DEFAULT_EDIT_ROLES
                                          + ", " + EdRecordSections.DB_PERCENT_WIDTH
                                          + "  )  \r\n"
                                          + "VALUES ("
                                          + "  " + EdRecordSections.PARM_FORM_GUID
                                          + ", " + EdRecordSections.PARM_NUMBER + "_" + count
                                          + ", " + EdRecordSections.PARM_ORDER + "_" + count
                                          + ", " + EdRecordSections.PARM_NAME + "_" + count
                                          + ", " + EdRecordSections.PARM_INSTRUCTIONS + "_" + count
                                          + ", " + EdRecordSections.PARM_FIELD_NAME + "_" + count
                                          + ", " + EdRecordSections.PARM_FIELD_VALUE + "_" + count
                                          + ", " + EdRecordSections.PARM_ON_MATCH_VISIBLE + "_" + count
                                          + ", " + EdRecordSections.PARM_VISIBLE + "_" + count
                                          + ", " + EdRecordSections.PARM_DEFAULT_DISPLAY_ROLES + "_" + count
                                          + ", " + EdRecordSections.PARM_DEFAULT_EDIT_ROLES + "_" + count
                                          + ", " + EdRecordSections.PARM_PERCENT_WIDTH + "_" + count + " );  \r\n");
            }

            if (parmList.Count > 1)
            {
                //
                // Convert the list to an array of SqlPararmeters.
                //
                SqlParameter [] parms = new SqlParameter [parmList.Count];

                for (int i = 0; i < parmList.Count; i++)
                {
                    parms [i] = parmList [i];
                }

                //
                // Extract the parameters
                //
                this.LogDebug(sbSQL_AddQuery.ToString( ));
                this.LogDebug(EvSqlMethods.getParameterSqlText(parms));

                //
                // Execute the update command.
                //
                try
                {
                    if (EvSqlMethods.QueryUpdate(sbSQL_AddQuery.ToString( ), parms) == 0)
                    {
                        return(EvEventCodes.Database_Record_Update_Error);
                    }
                }
                catch (Exception Ex)
                {
                    this.LogDebug(Evado.Model.EvStatics.getException(Ex));
                }
            } //END parameter list greater then 1
            return(EvEventCodes.Ok);
        }     //END UpdateItem method