/// <summary>
        /// Service callable code to get UserSettings
        /// </summary>
        /// <returns>A SettingsCD object with a UserSettings DataTable</returns>
        public static UserSettingsCD GetUserSettings(string userName)
        {
            if (string.IsNullOrEmpty(userName))
            {
                throw new Exception("The UserName is null or empty.");
            }

            using (new TransactionScope(TransactionScopeOption.Suppress))
            {
                var ds = new UserSettingsCD();
                var ta = new UserSettingsCDTableAdapters.UserSettingsTableAdapter();
                ta.Fill(ds.UserSettings, userName);
                return(ds);
            }
        }
        /// <summary>
        /// Service callable code to Delete/Insert/Update User Settings
        /// </summary>
        /// <param name="ds">A DataSet of type UserSettingsCD</param>
        /// <returns>A UserSettingsCD. If ALL updated OK contains updated data, if not contains the RowErrors</returns>
        public static UserSettingsCD UpdateUserSettings(DataSet ds)
        {
            if (ds == null || ds.Tables["UserSettings"] == null)
            {
                throw new Exception("The DataSet and/or DataTable is null.");
            }

            var clientDataSet = new UserSettingsCD();

            clientDataSet.Merge(ds, false, MissingSchemaAction.Ignore);

            if (clientDataSet.UserSettings.Select("", "", DataViewRowState.Deleted).Length > 0 ||
                clientDataSet.UserSettings.Select("", "", DataViewRowState.Added).Length > 0 ||
                clientDataSet.UserSettings.Select("", "", DataViewRowState.ModifiedCurrent).Length > 0)
            {
                var tblDataSet = new UserSettingsTD.UserSettingsDataTable();

                if (clientDataSet.UserSettings.Select("", "", DataViewRowState.ModifiedCurrent).Length > 0)
                {
                    var modifiedUserSetting = new UserSettingsDataTable();

                    foreach (UserSettingsRow modifiedRow in clientDataSet.UserSettings.Select("", "", DataViewRowState.ModifiedCurrent))
                    {
                        modifiedUserSetting.Clear();
                        modifiedUserSetting.ImportRow(modifiedRow);

                        tblDataSet.Merge(UserSettingsTD.GetUserSetting(modifiedRow.UserName));
                        tblDataSet.Merge(modifiedUserSetting, false, MissingSchemaAction.Ignore);
                    }
                }

                var userSettingsTD = new UserSettingsTD();
                userSettingsTD.UserSettings.BeginLoadData();
                userSettingsTD.UserSettings.Merge(clientDataSet.UserSettings, false, MissingSchemaAction.Ignore);
                userSettingsTD.UserSettings.Merge(tblDataSet, false, MissingSchemaAction.Ignore);

                using (var ts = new TransactionScope(TransactionScopeOption.Required))
                {
                    UserSettingsTD.UpdateUserSettings(userSettingsTD);
                    ts.Complete();
                }
            }

            return(clientDataSet);
        }
        /// <summary>
        /// Client callable code to update UserSettings.
        /// </summary>
        /// <param name="async">Do the update async.</param>
        /// <returns>True if update is successful. If False, check for RowErrors.</returns>
        public bool ClientUpdateUserSettings(bool async)
        {
            if (string.IsNullOrEmpty(_userName))
            {
                throw new Exception("The UserName and/or FormName is null or empty.");
            }

            PrepareDataBeforeUpdate();

            if (UserSettings.Select("", "", DataViewRowState.Deleted).Length > 0 ||
                UserSettings.Select("", "", DataViewRowState.Added).Length > 0 ||
                UserSettings.Select("", "", DataViewRowState.ModifiedCurrent).Length > 0)
            {
                var ds = new UserSettingsCD();
                foreach (UserSettingsRow deletedRow in UserSettings.Select("", "", DataViewRowState.Deleted))
                {
                    ds.UserSettings.ImportRow(deletedRow);
                }
                foreach (UserSettingsRow addedRow in UserSettings.Select("", "", DataViewRowState.Added))
                {
                    ds.UserSettings.ImportRow(addedRow);
                }
                foreach (UserSettingsRow modifiedCurrentRow in UserSettings.Select("", "", DataViewRowState.ModifiedCurrent))
                {
                    ds.UserSettings.ImportRow(modifiedCurrentRow);
                }

                if (UseWcfService)
                {
                    try
                    {
                        var svWCF = new WCF.SettingsContractClient();
                        var rqWCF = new WCF.UpdateUserSettingsRequest {
                            UserSettingsCD = new WCF.UserSettingsCD()
                        };
                        rqWCF.UserSettingsCD.Merge(ds, false, MissingSchemaAction.Ignore);

                        if (async)
                        {
                            if (String.IsNullOrEmpty(svWCF.Ping()))
                            {
                                throw new Exception("WCF is offline.");
                            }

                            svWCF.BeginUpdateUserSettings(rqWCF, wcf_ClientUpdateUserSettingsCompleted, svWCF);
                            return(true);
                        }

                        WCF.UpdateUserSettingsResponse rsWCF = svWCF.UpdateUserSettings(rqWCF);
                        Merge(rsWCF.UserSettingsCD, false, MissingSchemaAction.Ignore);
                        PrepareDataAfterUpdate();
                        return(true);
                    }
                    catch { UseWcfService = false; }                     // ignore if not responding
                }
                if (UseAsmxService)
                {
                    try
                    {
                        var svASMX = new ASMX.SettingsServiceASMX();
                        var rqASMX = new ASMX.UpdateUserSettingsRequest {
                            UserSettingsCD = new ASMX.UserSettingsCD()
                        };
                        rqASMX.UserSettingsCD.Merge(ds, false, MissingSchemaAction.Ignore);

                        if (async)
                        {
                            svASMX.UpdateUserSettingsCompleted += asmx_ClientUpdateUserSettingsCompleted;
                            svASMX.UpdateUserSettingsAsync(rqASMX);
                            return(true);
                        }

                        ASMX.UpdateUserSettingsResponse rsASMX = svASMX.UpdateUserSettings(rqASMX);
                        Merge(rsASMX.UserSettingsCD, false, MissingSchemaAction.Ignore);
                        PrepareDataAfterUpdate();
                        return(true);
                    }
                    catch { UseAsmxService = false; }                     // ignore if not responding
                }
                if (UseClientServer)
                {
                    try
                    {
                        Merge(UpdateUserSettings(ds), false, MissingSchemaAction.Ignore);
                        PrepareDataAfterUpdate();
                        return(true);
                    }
                    catch { UseClientServer = false; }                     // ignore if not responding
                }
            }
            return(false);
        }
        /// <summary>
        /// Client callable code to get UserSettings.
        /// </summary>
        /// <param name="userName">The User name</param>
        /// <param name="async">Do the update async</param>
        /// <returns>True if get successful.</returns>
        public bool ClientGetUserSettings(string userName, bool async)
        {
            if (string.IsNullOrEmpty(userName))
            {
                throw new Exception("The UserName is null or empty.");
            }

            _userName = userName;

            if (UseWcfService)
            {
                try
                {
                    var svWCF = new WCF.SettingsContractClient();
                    var rqWCF = new WCF.GetUserSettingsRequest {
                        UserName = userName
                    };

                    if (async)
                    {
                        if (String.IsNullOrEmpty(svWCF.Ping()))
                        {
                            throw new Exception("WCF is offline.");
                        }

                        svWCF.BeginGetUserSettings(rqWCF, wcf_ClientGetUserSettingsCompleted, svWCF);
                        return(true);
                    }
                    WCF.GetUserSettingsResponse rsWCF = svWCF.GetUserSettings(rqWCF);
                    PrepareDataAfterGet(rsWCF.UserSettingsCD);
                    Merge(rsWCF.UserSettingsCD, false, MissingSchemaAction.Ignore);
                    return(true);
                }
                catch { UseWcfService = false; }                 // ignore if not responding
            }
            if (UseAsmxService)
            {
                try
                {
                    var svASMX = new ASMX.SettingsServiceASMX();
                    var rqASMX = new ASMX.GetUserSettingsRequest {
                        UserName = userName
                    };

                    if (async)
                    {
                        svASMX.GetUserSettingsCompleted += asmx_ClientGetUserSettingsCompleted;
                        svASMX.GetUserSettingsAsync(rqASMX);
                        return(true);
                    }

                    ASMX.GetUserSettingsResponse rsASMX = svASMX.GetUserSettings(rqASMX);
                    PrepareDataAfterGet(rsASMX.UserSettingsCD);
                    Merge(rsASMX.UserSettingsCD, false, MissingSchemaAction.Ignore);
                    return(true);
                }
                catch { UseAsmxService = false; }                 // ignore if not responding
            }
            if (UseClientServer)
            {
                try
                {
                    UserSettingsCD userSettings = GetUserSettings(userName);
                    PrepareDataAfterGet(userSettings);
                    Merge(userSettings, false, MissingSchemaAction.Ignore);
                    return(true);
                }
                catch { UseClientServer = false; }                 // ignore if not responding
            }

            return(false);
        }