Example #1
0
        public StateModel GetStateInfo(int ID = 0)
        {
            ShomaRMEntities db    = new ShomaRMEntities();
            StateModel      model = new StateModel();

            model.ID           = 0;
            model.StateName    = "";
            model.Abbreviation = "";

            var stateInfo = db.tbl_State.Where(p => p.ID == ID).FirstOrDefault();

            if (stateInfo != null)
            {
                model.ID           = stateInfo.ID;
                model.StateName    = stateInfo.StateName;
                model.Abbreviation = stateInfo.Abbreviation;
            }
            return(model);
        }
Example #2
0
        public long SaveUpdateState(StateModel model)
        {
            ShomaRMEntities db             = new ShomaRMEntities();
            var             userNameExists = db.tbl_State.Where(p => p.ID != model.ID && p.StateName == model.StateName).FirstOrDefault();

            if (userNameExists == null)
            {
                if (model.ID == 0)
                {
                    var userData = new tbl_State()
                    {
                        StateName    = model.StateName,
                        Abbreviation = model.Abbreviation,
                    };
                    db.tbl_State.Add(userData);
                    db.SaveChanges();
                    model.ID = userData.ID;
                }
                else
                {
                    var stateInfo = db.tbl_State.Where(p => p.ID == model.ID).FirstOrDefault();
                    if (stateInfo != null)
                    {
                        stateInfo.StateName    = model.StateName;
                        stateInfo.Abbreviation = model.Abbreviation;
                        db.SaveChanges();
                    }
                    else
                    {
                        throw new Exception(model.StateName + " not exists in the system.");
                    }
                }
                return(model.ID);
            }
            else
            {
                throw new Exception(model.StateName + " already exists in the system.");
            }
        }