public void save(InstrumentSerialMaster InstrumentSerialMaster, string userId, bool isNew)
      {
          try
          {
              using (var context = new SmsMisDB())
              {
                  var entrymain = context.Entry(InstrumentSerialMaster);
                  if (entrymain != null)
                  {
                      InstrumentSerialMaster.AddDateTime            = DateTime.Now;
                      InstrumentSerialMaster.AddByUserId            = userId;
                      InstrumentSerialMaster.InstrumentSerialDetail = new List <InstrumentSerialDetail>();
                      for (int i = Convert.ToInt32(InstrumentSerialMaster.StartingInstrumentNo); i <= Convert.ToInt32(InstrumentSerialMaster.EndingInstrumentNo); i++)
                      {
                          InstrumentSerialDetail detail = new InstrumentSerialDetail();
                          detail.CompanyCode        = InstrumentSerialMaster.CompanyCode;
                          detail.BranchCode         = InstrumentSerialMaster.BranchCode;
                          detail.AccountCode        = InstrumentSerialMaster.AccountCode;
                          detail.Cancelled          = false;
                          detail.InstrumentTypeCode = InstrumentSerialMaster.InstrumentTypeCode;
                          detail.InstrumentNo       = i;
                          InstrumentSerialMaster.InstrumentSerialDetail.Add(detail);
                      }
                      if (isNew)
                      {
                          //InstrumentSerialMaster.InstrumentTypeSerial = Functions.getNextPk("InstrumentSerialMaster", "InstrumentTypeSerial", string.Concat(" WHERE CompanyCode=", InstrumentSerialMaster.CompanyCode, " AND BranchCode=", InstrumentSerialMaster.BranchCode, " AND AccountCode='", InstrumentSerialMaster.AccountCode, "' AND InstrumentTypeCode=", InstrumentSerialMaster.InstrumentTypeCode));
                          entrymain.State = EntityState.Added;
                      }
                      else
                      {
                          if (InstrumentSerialMaster.InstrumentSerialDetail != null && InstrumentSerialMaster.InstrumentSerialDetail.Count > 0)
                          {
                              InstrumentSerialMaster.InstrumentSerialDetail.ToList().ForEach(i => { i.CompanyCode = InstrumentSerialMaster.CompanyCode; });
                              InstrumentSerialMaster.InstrumentSerialDetail.ToList().ForEach(i => { i.BranchCode = InstrumentSerialMaster.BranchCode; });
                              InstrumentSerialMaster.InstrumentSerialDetail.ToList().ForEach(i => { i.AccountCode = InstrumentSerialMaster.AccountCode; });
                              InstrumentSerialMaster.InstrumentSerialDetail.ToList().ForEach(i => { i.InstrumentTypeCode = InstrumentSerialMaster.InstrumentTypeCode; });
                          }
                          entrymain.State = EntityState.Modified;
                      }
                      if (InstrumentSerialMaster.InstrumentSerialDetail != null && InstrumentSerialMaster.InstrumentSerialDetail.Count > 0)
                      {
                          InstrumentSerialMaster.InstrumentSerialDetail.ToList <InstrumentSerialDetail>().ForEach(entry => context.Entry(entry).State = EntityState.Added);
                      }
                      context.InstrumentSerialDetail.ToList().Where(i => i.BranchCode == InstrumentSerialMaster.BranchCode && i.CompanyCode == InstrumentSerialMaster.CompanyCode && i.AccountCode == InstrumentSerialMaster.AccountCode && i.InstrumentTypeCode == InstrumentSerialMaster.InstrumentTypeCode).ToList <InstrumentSerialDetail>().ForEach(entry => context.Entry(entry).State = EntityState.Deleted);

                      context.SaveChanges();
                  }
              }
          }
          catch (System.Data.Entity.Validation.DbEntityValidationException ex)
          {
              //throw ex;
          }
          catch (Exception ex)
          {
              // throw ex;
          }
      }
 public JsonResult deleteInstrumentSerialMaster(InstrumentSerialMaster InstrumentSerialMaster)
 {
     try
     {
         new hdlInstrumentSerialMaster().delete(InstrumentSerialMaster);
     }
     catch (Exception ex)
     {
         Response.StatusCode = 500;
         Content(JsonConvert.SerializeObject(new { error = ex.Message }));
     }
     return(getAllInstrumentSerialMaster(InstrumentSerialMaster.CompanyCode));
 }
 public JsonResult saveInstrumentSerialMaster(InstrumentSerialMaster InstrumentSerialMaster, bool isNew)
 {
     try
     {
         new hdlInstrumentSerialMaster().save(InstrumentSerialMaster, Convert.ToString(Session["User"]), isNew);
     }
     catch (Exception ex)
     {
         Response.StatusCode = 500;
         Content(JsonConvert.SerializeObject(new { error = ex.Message }));
     }
     return(getAllInstrumentSerialMaster(InstrumentSerialMaster.CompanyCode));
 }
 public void delete(InstrumentSerialMaster InstrumentSerialMaster)
 {
     try
     {
         var context = new SmsMisDB();
         context.InstrumentSerialMaster.Attach(InstrumentSerialMaster);
         var entry = context.Entry(InstrumentSerialMaster);
         if (entry != null)
         {
             entry.State = EntityState.Deleted;
             context.SaveChanges();
         }
     }
     catch (System.Data.Entity.Validation.DbEntityValidationException ex)
     {
         //throw SmsMis.Models.Console.Common.ExceptionTranslater.translate(ex);
         throw ex;
     }
     catch (Exception ex)
     {
         //throw SmsMis.Models.Console.Common.ExceptionTranslater.translate(ex);
         throw ex;
     }
 }