// GET: EmployeeAIs/Edit/5



        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            ///////
            int year = DateTime.Now.Year;
            var list = new List <int>();

            for (int i = 1950; i <= year; i++)
            {
                list.Add(i);
            }
            List <SelectListItem> YearOfGraduation1 = new List <SelectListItem>();

            //YearOfGraduation1.Add(new SelectListItem { Text = "Select", Value = "0" });
            for (int i = 1950; i <= year; i++)
            {
                YearOfGraduation1.Add(new SelectListItem {
                    Text = i.ToString(), Value = i.ToString()
                });
            }
            ViewBag.YearOfGraduation1s = new SelectList(YearOfGraduation1, "Text", "Text");
            ViewBag.YearOfGraduation2s = new SelectList(YearOfGraduation1, "Text", "Text");
            ViewBag.YearOfGraduation3s = new SelectList(YearOfGraduation1, "Text", "Text");

            ///////
            ViewBag.Qualification1s = new SelectList(db.QualificationMasters, "QualificationName", "QualificationName");
            ViewBag.Qualification2s = new SelectList(db.QualificationMasters, "QualificationName", "QualificationName");
            ViewBag.Qualification3s = new SelectList(db.QualificationMasters, "QualificationName", "QualificationName");
            EmployeeAI employeeAI = db.EmployeeAIs.Find(id);

            if (employeeAI == null)
            {
                return(HttpNotFound());
            }
            EmployeeAll empall = new EmployeeAll();

            var empid = (from a in db.EmployeeAIs where a.EmployeeAIId == id select a.EmployeeCode).FirstOrDefault();

            empall.employeeai          = (from a in db.EmployeeAIs where a.EmployeeAIId == id select a).ToList();
            empall.employeeassociation = (from a in db.EmpAIAssociations where a.EmployeeCode == empid select a).ToList();
            empall.employeeconference  = (from a in db.EmpAIConferences where a.EmployeeCode == empid select a).ToList();



            ViewBag.EmployeeCode = new SelectList(db.EmployeeGIs, "EmployeeCode", "EmployeeCode", employeeAI.EmployeeCode);
            return(View(empall));
            //return View(employeeAI);
        }
        // POST: EmployeeAIs/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        //[HttpPost]
        //[ValidateAntiForgeryToken]
        //public ActionResult Edit([Bind(Include = "EmployeeAIId,EmployeeCode,InstitutionAttended1,InstitutionAttended2,InstitutionAttended3,Qualification1,Qualification2,Qualification3,YearOfGraduation1,YearOfGraduation2,YearOfGraduation3,ProfessionalAssociationsTitle,ProfessionalAssociationsIdNo,ProfessionalAssociationsDate,ConferenceAttendedName,ConferenceAttendedTitle,ConferenceAttendedDate,CreatedDate,IsDeleted")] EmployeeAI employeeAI)
        //{
        //    try {
        //    if (ModelState.IsValid)
        //    {
        //        employeeAI.EmployeeAIId = (from a in db.EmployeeAIs where a.EmployeeCode == employeeAI.EmployeeCode select a.EmployeeAIId).FirstOrDefault();
        //        employeeAI.CreatedDate = DateTime.Now;
        //        employeeAI.IsDeleted = false;

        //        db.Entry(employeeAI).State = EntityState.Modified;
        //        db.SaveChanges();
        //     }
        //    ViewBag.EmployeeCode = new SelectList(db.EmployeeGIs, "EmployeeCode", "EmployeeCode", employeeAI.EmployeeCode);
        //        TempData["successmsg"] = "Record is Successfully Updated";
        //        return RedirectToAction("Index");
        //    }
        //    catch (Exception ext)
        //    {
        //        TempData["msg"] = "Record is not Updated,Try again";

        //    }

        //    return View(employeeAI);
        //}

        // GET: EmployeeAIs/Delete/5
        public ActionResult Delete(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            EmployeeAI employeeAI = db.EmployeeAIs.Find(id);

            if (employeeAI == null)
            {
                return(HttpNotFound());
            }
            return(View(employeeAI));
        }
 public ActionResult DeleteConfirmed(int id)
 {
     try
     {
         EmployeeAI employeeAI = db.EmployeeAIs.Find(id);
         db.EmployeeAIs.Remove(employeeAI);
         db.SaveChanges();
         TempData["successmsg"] = "Record is Successfully Deleted";
         return(RedirectToAction("Index"));
     }
     catch (Exception ext)
     {
         TempData["msg"] = "Record is not Deleted,Try again";
     }
     return(RedirectToAction("Index"));
 }
        public JsonResult InsertCustomers(EmployeeAI obj, List <EmpAIAssociation> ObjAssociation, List <EmpAIConference> ObjConferenece)
        {
            int insertedRecords = 0;

            try
            {
                using (IARTDBNEWEntities entities = new IARTDBNEWEntities())
                {
                    int i = (from a in entities.EmployeeAIs where a.EmployeeCode == obj.EmployeeCode select a).Count();
                    if (i == 0)
                    {
                        entities.EmployeeAIs.Add(obj);
                    }

                    //Check for NULL.
                    if (ObjAssociation == null)
                    {
                        ObjAssociation = new List <EmpAIAssociation>();
                    }

                    //Loop and insert records.
                    foreach (EmpAIAssociation item in ObjAssociation)
                    {
                        entities.EmpAIAssociations.Add(item);
                    }


                    if (ObjConferenece == null)
                    {
                        ObjConferenece = new List <EmpAIConference>();
                    }

                    //Loop and insert records.
                    foreach (EmpAIConference item in ObjConferenece)
                    {
                        entities.EmpAIConferences.Add(item);
                    }

                    insertedRecords = entities.SaveChanges();
                }
            }
            catch (Exception ext)
            {
            }
            return(Json(insertedRecords));
        }
        public ActionResult Create([Bind(Include = "CustomerId,EmployeeAIId,EmployeeCode,InstitutionAttended1,InstitutionAttended2,InstitutionAttended3,Qualification1,Qualification2,Qualification3,YearOfGraduation1,YearOfGraduation2,YearOfGraduation3,ProfessionalAssociationsTitle,ProfessionalAssociationsIdNo,ProfessionalAssociationsDate,ConferenceAttendedName,ConferenceAttendedTitle,ConferenceAttendedDate,CreatedDate,IsDeleted")] EmployeeAI employeeAI)
        {
            try
            {
                EmpAIAssociation empa = new Models.EmpAIAssociation();
                EmpAIConference  empc = new Models.EmpAIConference();
                if (ModelState.IsValid)
                {
                    // string strConnString = "IARTDBNEWEntities"; // get it from Web.config file
                    // SqlTransaction objTrans = null;

                    using (System.Data.Entity.DbContextTransaction objTrans = db.Database.BeginTransaction())
                    {
                        {
                            int empidcount = (from a in db.EmployeeAIs where a.EmployeeCode == employeeAI.EmployeeCode select a).Count();
                            if (empidcount == 0)
                            {
                                employeeAI.CreatedDate = DateTime.Now.Date;
                                db.EmployeeAIs.Add(employeeAI);
                                db.SaveChanges();

                                //association
                                empa.AttendedDate = employeeAI.ProfessionalAssociationsDate;
                                empa.IDnumber     = employeeAI.ProfessionalAssociationsIdNo;
                                empa.EmployeeCode = employeeAI.EmployeeCode;

                                empa.Title       = employeeAI.ProfessionalAssociationsTitle;
                                empa.IsDeleted   = false;
                                empa.CreatedDate = DateTime.Now.Date;
                                db.EmpAIAssociations.Add(empa);
                                db.SaveChanges();
                                //////Prof summary

                                empc.AttendedDate = employeeAI.ConferenceAttendedDate;
                                empc.Name         = employeeAI.ConferenceAttendedName;
                                empc.EmployeeCode = employeeAI.EmployeeCode;
                                empc.Title        = employeeAI.ConferenceAttendedTitle;
                                empc.IsDeleted    = false;
                                empc.CreatedDate  = DateTime.Now.Date;
                                db.EmpAIConferences.Add(empc);
                                db.SaveChanges();
                            }

                            try
                            {
                                objTrans.Commit();
                            }
                            catch (Exception)
                            {
                                objTrans.Rollback();
                            }
                            finally
                            {
                            }

                            ////////


                            ViewBag.EmployeeCode   = new SelectList(db.EmployeeGIs, "EmployeeCode", "EmployeeCode", employeeAI.EmployeeCode);
                            TempData["successmsg"] = "Record is Successfully Added";
                            return(RedirectToAction("Index"));
                        }
                    }
                }
            }
            catch (Exception ext)
            {
                TempData["msg"] = "Record is not Added,Try again";
            }
            return(View(employeeAI));
        }
        public JsonResult Edit(EmployeeAI obj, List <EmpAIAssociation> ObjAssociation, List <EmpAIConference> ObjConferenece)
        {
            int insertedRecords = 0;

            try
            {
                var        id       = (from a in db.EmployeeAIs where a.EmployeeCode == obj.EmployeeCode select a.EmployeeAIId).FirstOrDefault();
                var        idgI     = (from a in db.EmployeeGIs where a.EmployeeCode == obj.EmployeeCode select a.EmployeeGIId).FirstOrDefault();
                EmployeeGI objempGi = db.EmployeeGIs.Find(idgI);
                EmployeeAI objdest  = db.EmployeeAIs.Find(id);

                objdest.EmployeeCode                  = obj.EmployeeCode;
                objdest.InstitutionAttended1          = obj.InstitutionAttended1;
                objdest.InstitutionAttended2          = obj.InstitutionAttended2;
                objdest.InstitutionAttended3          = obj.InstitutionAttended3;
                objdest.Qualification1                = obj.Qualification1;
                objdest.Qualification2                = obj.Qualification2;
                objdest.Qualification3                = obj.Qualification3;
                objdest.YearOfGraduation1             = obj.YearOfGraduation1;
                objdest.YearOfGraduation2             = obj.YearOfGraduation2;
                objdest.YearOfGraduation3             = obj.YearOfGraduation3;
                objdest.CreatedDate                   = objdest.CreatedDate;
                objdest.IsDeleted                     = objdest.IsDeleted;
                objdest.ConferenceAttendedDate        = DateTime.Now.Date;
                objdest.ConferenceAttendedName        = "NA";
                objdest.ConferenceAttendedTitle       = "NA";
                objdest.ProfessionalAssociationsDate  = DateTime.Now.Date;
                objdest.ProfessionalAssociationsIdNo  = "1";
                objdest.ProfessionalAssociationsTitle = "NA";
                objdest.EmployeeGI                    = objempGi;
                db.SaveChanges();
                int i = (from a in db.EmployeeAIs where a.EmployeeCode == obj.EmployeeCode select a).Count();
                if (i == 0)
                {
                    //entities.EmployeeAIs.Add(obj);

                    //Check for NULL.
                    if (ObjAssociation == null)
                    {
                        ObjAssociation = new List <EmpAIAssociation>();
                    }
                }

                //Loop and insert records.
                foreach (EmpAIAssociation item in ObjAssociation)
                {
                    EmpAIAssociation Existed_Emp = db.EmpAIAssociations.Find(item.AssociationsId);
                    Existed_Emp.IDnumber     = item.IDnumber;
                    Existed_Emp.Title        = item.Title;
                    Existed_Emp.AttendedDate = item.AttendedDate;
                    Existed_Emp.IsDeleted    = false;
                }
                db.SaveChanges();

                if (ObjConferenece == null)
                {
                    ObjConferenece = new List <EmpAIConference>();
                }

                //Loop and insert records.
                foreach (EmpAIConference item in ObjConferenece)
                {
                    EmpAIConference Existed_Emp = db.EmpAIConferences.Find(item.ConferenceId);
                    Existed_Emp.Name         = item.Name;
                    Existed_Emp.Title        = item.Title;
                    Existed_Emp.AttendedDate = item.AttendedDate;
                    Existed_Emp.IsDeleted    = false;
                }
                db.SaveChanges();

                insertedRecords = id;
            }

            catch (Exception ext)
            {
            }
            return(Json(insertedRecords));
        }