public tblSettingsL1 SaveSettingL1(string SettingName, object SettingValue, int CompanyID, dbVisionEntities db, SavingResult res) { tblSettingMasterL1 SaveModelMaster = db.tblSettingMasterL1.FirstOrDefault(r => r.SettingName == SettingName); if (SaveModelMaster == null) { res.ValidationError = "Setting not found."; res.ExecutionResult = eExecutionResult.ValidationError; return(null); } tblSettingsL1 SaveModel = db.tblSettingsL1.FirstOrDefault(r => r.SettingName == SettingName && r.CompanyID == CompanyID); if (SaveModel == null) // New Entry { SaveModel = new tblSettingsL1() { SettingName = SaveModelMaster.SettingName, CompanyID = CompanyID }; SaveModel.rcuid = Model.CommonProperties.LoginInfo.LoggedinUser.UserID; SaveModel.rcdt = DateTime.Now; db.tblSettingsL1.Add(SaveModel); } else { SaveModel.redt = DateTime.Now; db.tblSettingsL1.Attach(SaveModel); db.Entry(SaveModel).State = System.Data.Entity.EntityState.Modified; } switch ((eSettingValueType)SaveModelMaster.SettingValueType) { case eSettingValueType.Varchar50: SaveModel.SettingValueVC = (string)SettingValue; break; case eSettingValueType.Int: /// specially for enum type, casted to int then nullable int SaveModel.SettingValueInt = (SettingValue != null ? (int?)((int)SettingValue) : null); break; case eSettingValueType.Long: SaveModel.SettingValueLong = (long?)SettingValue; break; case eSettingValueType.DateTime: SaveModel.SettingValueDateTime = (DateTime?)SettingValue; break; case eSettingValueType.Boolean: SaveModel.SettingValueBoolean = (bool?)SettingValue; break; case eSettingValueType.Decimal: SaveModel.SettingValueDecimal = (decimal?)SettingValue; break; } return(SaveModel); }
public SavingResult SaveSettingL1(string SettingName, object SettingValue, int CompanyID) { SavingResult res = new SavingResult(); //-- using (dbVisionEntities db = new dbVisionEntities()) { tblSettingsL1 SaveModel = SaveSettingL1(SettingName, SettingValue, CompanyID, db, res); if (res.ExecutionResult == eExecutionResult.ValidationError) { return(res); } //-- try { db.SaveChanges(); res.PrimeKeyValue = SaveModel.SettingL1ID; res.ExecutionResult = eExecutionResult.CommitedSucessfuly; } catch (Exception ex) { CommonFunctions.GetFinalError(res, ex); } } return(res); }
public object GetSettingL1(string SettingName, long ComapnyID) { using (dbVisionEntities db = new dbVisionEntities()) { tblSettingMasterL1 SaveModelMaster = db.tblSettingMasterL1.FirstOrDefault(r => r.SettingName == SettingName); if (SaveModelMaster == null) { throw new Exception($"Setting name not found. Setting Name : {SettingName}"); } tblSettingsL1 SaveModel = db.tblSettingsL1.FirstOrDefault(r => r.SettingName == SettingName && r.CompanyID == ComapnyID); //object SettingValue = null; if (SaveModel == null) { if (SaveModelMaster != null) { switch ((eSettingValueType)SaveModelMaster.SettingValueType) { case eSettingValueType.Varchar50: return(SaveModelMaster.SettingValueVC); case eSettingValueType.Int: return(SaveModelMaster.SettingValueInt); case eSettingValueType.Long: return(SaveModelMaster.SettingValueLong); case eSettingValueType.DateTime: return(SaveModelMaster.SettingValueDateTime); case eSettingValueType.Boolean: return(SaveModelMaster.SettingValueBoolean); case eSettingValueType.Decimal: return(SaveModelMaster.SettingValueDecimal); } } } else { switch ((eSettingValueType)SaveModelMaster.SettingValueType) { case eSettingValueType.Varchar50: return(SaveModel.SettingValueVC); case eSettingValueType.Int: return(SaveModel.SettingValueInt); case eSettingValueType.Long: return(SaveModel.SettingValueLong); case eSettingValueType.DateTime: return(SaveModel.SettingValueDateTime); case eSettingValueType.Boolean: return(SaveModel.SettingValueBoolean); case eSettingValueType.Decimal: return(SaveModel.SettingValueDecimal); } } } return(null); }
public void GetSetting(long CompanyID, ApplicationSettingsViewModel NewSettings) { if (NewSettings == null) { return; } using (dbVisionEntities db = new dbVisionEntities()) { PropertyInfo[] Props = typeof(ApplicationSettingsViewModel).GetProperties(); foreach (var SaveModel in db.tblSettingL0) { string SettingName = SaveModel.SettingName; PropertyInfo p = Props.FirstOrDefault(r => r.Name == SettingName); if (p == null) { continue; } eSettingValueType SettingValueType = (eSettingValueType)SaveModel.SettingValueType; object SettingValue = (SettingValueType == eSettingValueType.Varchar50 ? (object)SaveModel.SettingValueVC : (SettingValueType == eSettingValueType.Int ? (object)SaveModel.SettingValueInt : (SettingValueType == eSettingValueType.Long ? (object)SaveModel.SettingValueLong : (SettingValueType == eSettingValueType.DateTime ? (object)SaveModel.SettingValueDateTime : (SettingValueType == eSettingValueType.Boolean ? (object)SaveModel.SettingValueBoolean : (SettingValueType == eSettingValueType.Decimal ? (object)SaveModel.SettingValueDecimal : null)))))); SettingValue = (SettingValueType == eSettingValueType.Int && p.PropertyType == typeof(int) ? (object)((int?)SettingValue ?? 0) : (SettingValueType == eSettingValueType.Long && p.PropertyType == typeof(long) ? (object)((long?)SettingValue ?? 0) : (SettingValueType == eSettingValueType.DateTime && p.PropertyType == typeof(DateTime) ? (object)((DateTime?)SettingValue ?? DateTime.MinValue) : (SettingValueType == eSettingValueType.Boolean && p.PropertyType == typeof(bool) ? (object)((bool?)SettingValue ?? false) : (SettingValueType == eSettingValueType.Decimal && p.PropertyType == typeof(decimal) ? (object)((decimal?)SettingValue ?? 0) : SettingValue))))); p.SetValue(NewSettings, SettingValue); } //foreach (PropertyInfo p in Props) foreach (var SaveModelMaster in db.tblSettingMasterL1) { string SettingName = SaveModelMaster.SettingName; PropertyInfo p = Props.FirstOrDefault(r => r.Name == SettingName); if (p == null) { continue; } //tblSettingMasterL1 SaveModelMaster = db.tblSettingMasterL1.FirstOrDefault(r => r.SettingName == SettingName); //if (SaveModelMaster == null) //{ // continue; //} eSettingValueType SettingValueType = (eSettingValueType)SaveModelMaster.SettingValueType; object SettingMasterValue = (SettingValueType == eSettingValueType.Varchar50 ? (object)SaveModelMaster.SettingValueVC : (SettingValueType == eSettingValueType.Int ? (object)SaveModelMaster.SettingValueInt : (SettingValueType == eSettingValueType.Long ? (object)SaveModelMaster.SettingValueLong : (SettingValueType == eSettingValueType.DateTime ? (object)SaveModelMaster.SettingValueDateTime : (SettingValueType == eSettingValueType.Boolean ? (object)SaveModelMaster.SettingValueBoolean : (SettingValueType == eSettingValueType.Decimal ? (object)SaveModelMaster.SettingValueDecimal : null)))))); object SettingValue = null; if (CompanyID != 0) { tblSettingsL1 SaveModel = db.tblSettingsL1.FirstOrDefault(r => r.SettingName == SettingName && r.CompanyID == CompanyID); SettingValue = (SaveModel == null ? null : (SettingValueType == eSettingValueType.Varchar50 ? (object)SaveModel.SettingValueVC : (SettingValueType == eSettingValueType.Int ? (object)SaveModel.SettingValueInt : (SettingValueType == eSettingValueType.Long ? (object)SaveModel.SettingValueLong : (SettingValueType == eSettingValueType.DateTime ? (object)SaveModel.SettingValueDateTime : (SettingValueType == eSettingValueType.Boolean ? (object)SaveModel.SettingValueBoolean : (SettingValueType == eSettingValueType.Decimal ? (object)SaveModel.SettingValueDecimal : null))))))); } if (SettingValue == null) { SettingValue = SettingMasterValue; } /// If property type is not nullable type and setting value is null then assign default value. SettingValue = (SettingValueType == eSettingValueType.Int && (p.PropertyType == typeof(int) || p.PropertyType.IsEnum) ? (object)((int?)SettingValue ?? 0) : (SettingValueType == eSettingValueType.Long && p.PropertyType == typeof(long) ? (object)((long?)SettingValue ?? 0) : (SettingValueType == eSettingValueType.DateTime && p.PropertyType == typeof(DateTime) ? (object)((DateTime?)SettingValue ?? DateTime.MinValue) : (SettingValueType == eSettingValueType.Boolean && p.PropertyType == typeof(bool) ? (object)((bool?)SettingValue ?? false) : (SettingValueType == eSettingValueType.Decimal && p.PropertyType == typeof(decimal) ? (object)((decimal?)SettingValue ?? 0) : SettingValue))))); p.SetValue(NewSettings, SettingValue); } } }