//[HttpPost]
        //public ActionResult Create(OperatorModule OPTDet)
        //{
        //    if ((Session["UserId"] == null) || (Session["UserId"].ToString() == String.Empty))
        //    {
        //        return RedirectToAction("Login", "Login", null);
        //    }
        //    ViewBag.Logout = Session["Username"].ToString().ToUpper();
        //    ViewBag.roleid = Session["RoleID"];
        //    int UserID = Convert.ToInt32(Session["UserId"]);
        //    string Dept = OPTDet.OPDetails.Dept;
        //    string OperatorName = OPTDet.OPDetails.OperatorName;
        //    int OperatorID = OPTDet.OPDetails.OperatorID;
        //    bool check = ValidationCheckForInsertion(OperatorID);
        //    if (check == true)
        //    {
        //        OPTDet.OPDetails.CreatedOn = DateTime.Now;
        //        OPTDet.OPDetails.CorrectedDate = DateTime.Now.Date;
        //        OPTDet.OPDetails.CreatedBy = UserID;
        //        OPTDet.OPDetails.isDeleted = 0;
        //        db.tbloperatordetails.Add(OPTDet.OPDetails);
        //        db.SaveChanges();
        //    }
        //    return RedirectToAction("Index");
        //}

        public string CreateOperator(string dept, string operatorId, string operatorName, int OperatorDesc)
        {
            string res    = "";
            int    UserID = Convert.ToInt32(Session["UserId"]);
            bool   check  = ValidationCheckForInsertion(operatorId);

            if (check == true)
            {
                tbloperatordetail OPTDet = new tbloperatordetail();
                OPTDet.CreatedOn     = DateTime.Now;
                OPTDet.CorrectedDate = DateTime.Now.Date;
                OPTDet.CreatedBy     = UserID;
                OPTDet.isDeleted     = 0;
                OPTDet.Dept          = dept;
                OPTDet.OperatorID    = Convert.ToString(operatorId);
                OPTDet.OperatorName  = operatorName;
                OPTDet.OperatorDesc  = OperatorDesc;
                db.tbloperatordetails.Add(OPTDet);
                db.SaveChanges();
                res = "Success";
            }
            else
            {
                res = "failure";
            }
            return(res);
        }
        // GET: OperatorModule
        public ActionResult Index()
        {
            if ((Session["UserId"] == null) || (Session["UserId"].ToString() == String.Empty))
            {
                return(RedirectToAction("Login", "Login", null));
            }
            ViewBag.Logout = Session["Username"].ToString().ToUpper();
            ViewBag.roleid = Session["RoleID"];
            String Username = Session["Username"].ToString();

            OperatorModule           OPList    = new OperatorModule();
            tbloperatordetail        OPDet     = new tbloperatordetail();
            List <tbloperatordetail> OPDetList = new List <tbloperatordetail>();

            OPDetList            = db.tbloperatordetails.Where(m => m.isDeleted == 0).ToList();
            OPList.OPdetailsList = OPDetList;
            return(View(OPList));
        }
        public ActionResult Delete(int id)
        {
            if ((Session["UserId"] == null) || (Session["UserId"].ToString() == String.Empty))
            {
                return(RedirectToAction("Login", "Login", null));
            }
            ViewBag.Logout = Session["Username"].ToString().ToUpper();
            ViewBag.roleid = Session["RoleID"];
            int               UserID   = Convert.ToInt32(Session["UserId"]);
            String            Username = Session["Username"].ToString();
            tbloperatordetail OpTDet   = db.tbloperatordetails.Find(id);

            OpTDet.isDeleted       = 1;
            OpTDet.ModifiedBy      = UserID;
            OpTDet.ModifiedOn      = DateTime.Now;
            db.Entry(OpTDet).State = System.Data.Entity.EntityState.Modified;
            db.SaveChanges();
            return(RedirectToAction("Index"));
        }
        public ActionResult Edit(int id)
        {
            if ((Session["UserId"] == null) || (Session["UserId"].ToString() == String.Empty))
            {
                return(RedirectToAction("Login", "Login", null));
            }
            ViewBag.Logout = Session["Username"];
            ViewBag.roleid = Session["RoleID"];
            String Username = Session["Username"].ToString();

            Session["Opid"] = id;
            tbloperatordetail tblmc = db.tbloperatordetails.Find(id);

            if (tblmc == null)
            {
                return(HttpNotFound());
            }
            ViewBag.Operator = new SelectList(db.tblroles.Where(m => m.IsDeleted == 0), "Role_ID", "RoleDesc", tblmc.OperatorDesc);
            return(View(tblmc));
        }
        //[HttpPost]
        //public ActionResult Edit(OperatorModule OPDet, int hdnpid = 0)
        //{
        //    if ((Session["UserId"] == null) || (Session["UserId"].ToString() == String.Empty))
        //    {
        //        return RedirectToAction("Login", "Login", null);
        //    }
        //    ViewBag.Logout = Session["Username"].ToString().ToUpper();
        //    ViewBag.roleid = Session["RoleID"];
        //    int UserID = Convert.ToInt32(Session["UserId"]);
        //    string Dept = OPDet.OPDetails.Dept;
        //    string OperatorName = OPDet.OPDetails.OperatorName;
        //    int OperatorID = OPDet.OPDetails.OperatorID;
        //    int OPID = hdnpid;
        //    bool check = ValidationCheckForInsertion(OperatorID);
        //    if (check == true)
        //    {
        //        //tbloperatordetail obj = db.tbloperatordetails.Find(OPID);
        //        tbloperatordetail obj = new tbloperatordetail();
        //        obj.Dept = OPDet.OPDetails.Dept;
        //        obj.OperatorName = OPDet.OPDetails.OperatorName;
        //        obj.OperatorID = OPDet.OPDetails.OperatorID;
        //        obj.CreatedOn = DateTime.Now;
        //        obj.CreatedBy = UserID;
        //        obj.CorrectedDate = DateTime.Now.Date;
        //        obj.isDeleted = 0;
        //        db.tbloperatordetails.Add(obj);
        //        db.SaveChanges();
        //    }
        //    else
        //    {
        //        tbloperatordetail obj = db.tbloperatordetails.Find(OPID);
        //        obj.Dept = OPDet.OPDetails.Dept;
        //        obj.OperatorName = OPDet.OPDetails.OperatorName;
        //        obj.ModifiedOn = DateTime.Now;
        //        obj.ModifiedBy = UserID;
        //        db.SaveChanges();
        //    }
        //    return RedirectToAction("Index");

        //}


        public string EditOperator(int id, string dept, int operatorId, string operatorName, int OperatorDesc)
        {
            string res    = "";
            int    UserID = Convert.ToInt32(Session["UserId"]);
            bool   check  = ValidationCheckForUpdation(id);

            if (check == true)
            {
                res = "failure";
            }
            else
            {
                tbloperatordetail obj = db.tbloperatordetails.Find(id);
                obj.Dept         = dept;
                obj.OperatorName = operatorName;
                obj.OperatorDesc = OperatorDesc;
                obj.OperatorID   = Convert.ToString(operatorId);
                obj.ModifiedOn   = DateTime.Now;
                obj.ModifiedBy   = UserID;
                db.SaveChanges();
                res = "Success";
            }
            return(res);
        }
        public ActionResult ImportOperatorData(HttpPostedFileBase file, string UploadType)
        {
            if ((Session["UserId"] == null) || (Session["UserId"].ToString() == String.Empty))
            {
                return(RedirectToAction("Login", "Login", null));
            }
            string UserId = Session["UserId"].ToString();

            //Deleting Excel file
            #region
            string        fileLocation1 = Server.MapPath("~/Content/");
            DirectoryInfo di            = new DirectoryInfo(fileLocation1);
            FileInfo[]    files         = di.GetFiles("*.xlsx").Where(p => p.Extension == ".xlsx").ToArray();
            foreach (FileInfo file1 in files)
            {
                try
                {
                    file1.Attributes = FileAttributes.Normal;
                    System.IO.File.Delete(file1.FullName);
                }
                catch { }
            }
            #endregion

            DataSet ds = new DataSet();
            if (Request.Files["file"].ContentLength > 0)
            {
                string fileExtension = System.IO.Path.GetExtension(Request.Files["file"].FileName);
                if (fileExtension == ".xls" || fileExtension == ".xlsx")
                {
                    string fileLocation = Server.MapPath("~/Content/") + Request.Files["file"].FileName;
                    if (System.IO.File.Exists(fileLocation))
                    {
                        System.IO.File.Delete(fileLocation);
                    }
                    Request.Files["file"].SaveAs(fileLocation);
                    string excelConnectionString = string.Empty;
                    excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
                                            fileLocation + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
                    //connection String for xls file format.
                    if (fileExtension == ".xls")
                    {
                        excelConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
                                                fileLocation + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
                    }
                    //connection String for xlsx file format.
                    else if (fileExtension == ".xlsx")
                    {
                        excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
                                                fileLocation + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
                    }
                    //Create Connection to Excel work book and add oledb namespace
                    OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);
                    excelConnection.Open();
                    DataTable dt = new DataTable();
                    dt = excelConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                    if (dt == null)
                    {
                        return(null);
                    }
                    String[] excelSheets = new String[dt.Rows.Count];
                    int      t           = 0;
                    //excel data saves in temp file here.
                    foreach (DataRow row in dt.Rows)
                    {
                        excelSheets[t] = row["TABLE_NAME"].ToString();
                        t++;
                    }
                    OleDbConnection excelConnection1 = new OleDbConnection(excelConnectionString);
                    string          query            = string.Format("Select * from [{0}]", excelSheets[0]);
                    using (OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, excelConnection1))
                    {
                        dataAdapter.Fill(ds);
                    }
                    excelConnection.Close();
                    excelConnection1.Close();
                }
                if (fileExtension.ToString().ToLower().Equals(".xml"))
                {
                    string fileLocation = Server.MapPath("~/Content/") + Request.Files["FileUpload"].FileName;
                    if (System.IO.File.Exists(fileLocation))
                    {
                        System.IO.File.Delete(fileLocation);
                    }
                    Request.Files["FileUpload"].SaveAs(fileLocation);
                    XmlTextReader xmlreader = new XmlTextReader(fileLocation);
                    // DataSet ds = new DataSet();
                    ds.ReadXml(xmlreader);
                    xmlreader.Close();
                }
                if ((Session["UserId"] == null) || (Session["UserId"].ToString() == String.Empty))
                {
                    return(RedirectToAction("Login", "Login", null));
                }
                ViewBag.Logout = Session["Username"].ToString().ToUpper();
                ViewBag.roleid = Session["RoleID"];


                string text = "";

                string ErrorMsg = null;

                if (UploadType == "New") // Delete Duplicate and Insert New. // if not Duplicate insert that
                {
                    #region
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        tbloperatordetail tblmp    = new tbloperatordetail();
                        String            Username = Session["Username"].ToString();
                        tblmp.CreatedBy = Convert.ToInt32(Session["UserId"]);
                        tblmp.CreatedOn = DateTime.Now;
                        tblmp.isDeleted = 0;

                        string Dept = null, OperatorName = null;
                        string OperatorID = null;
                        Dept         = Convert.ToString(ds.Tables[0].Rows[i][0]);
                        OperatorName = Convert.ToString(ds.Tables[0].Rows[i][1]);
                        OperatorID   = Convert.ToString(ds.Tables[0].Rows[i][2]);

                        bool check = ValidationCheckForInsertion(OperatorID);
                        if (check == true)
                        {
                            using (i_facility_shaktiEntities1 db = new i_facility_shaktiEntities1())
                            {
                                if (string.IsNullOrEmpty(Dept) || string.IsNullOrEmpty(OperatorName) || OperatorID == null)
                                {
                                    text = text + htmlerrorMaker(Dept, OperatorName, OperatorID, "Dept, OperatorName or OperatorID cannot be empty/Check the format");
                                    continue;
                                }
                                else
                                {
                                    try
                                    {
                                        tblmp.Dept         = Dept.Trim();
                                        tblmp.OperatorName = OperatorName.Trim();
                                        tblmp.OperatorID   = OperatorID;
                                        tblmp.CreatedBy    = Convert.ToInt32(UserId);
                                        tblmp.CreatedOn    = DateTime.Now;
                                    }
                                    catch
                                    {
                                    }
                                }
                            }

                            db.tbloperatordetails.Add(tblmp);
                            try
                            {
                                db.SaveChanges();
                            }
                            catch (DbEntityValidationException e)
                            {
                                foreach (var eve in e.EntityValidationErrors)
                                {
                                    Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                                      eve.Entry.Entity.GetType().Name, eve.Entry.State);
                                    foreach (var ve in eve.ValidationErrors)
                                    {
                                        Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                                                          ve.PropertyName, ve.ErrorMessage);
                                    }
                                }
                                throw;
                            }
                        }
                    }
                    #endregion
                }
                else if (UploadType == "Update") // Delete Duplicate and Insert New. // if not Duplicate insert that
                {
                    #region
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        tbloperatordetail tblmp    = new tbloperatordetail();
                        String            Username = Session["Username"].ToString();
                        tblmp.CreatedBy = Convert.ToInt32(Session["UserId"]);
                        tblmp.CreatedOn = DateTime.Now;
                        tblmp.isDeleted = 0;

                        string Dept = null, OperatorName = null;
                        string OperatorID = null;
                        Dept         = Convert.ToString(ds.Tables[0].Rows[i][0]);
                        OperatorName = Convert.ToString(ds.Tables[0].Rows[i][1]);
                        OperatorID   = Convert.ToString(ds.Tables[0].Rows[i][2]);

                        bool check = ValidationCheckForInsertion(OperatorID);
                        if (check == true)
                        {
                            using (i_facility_shaktiEntities1 db1 = new i_facility_shaktiEntities1())
                            {
                                if (string.IsNullOrEmpty(Dept) || string.IsNullOrEmpty(OperatorName) || OperatorID == null)
                                {
                                    text = text + htmlerrorMaker(Dept, OperatorName, OperatorID, "Dept, OperatorName or OperatorID cannot be empty/Check the format");
                                    continue;
                                }
                                else
                                {
                                    try
                                    {
                                        tblmp.Dept         = Dept.Trim();
                                        tblmp.OperatorName = OperatorName.Trim();
                                        tblmp.OperatorID   = OperatorID;
                                        tblmp.CreatedBy    = Convert.ToInt32(UserId);
                                        tblmp.CreatedOn    = DateTime.Now;
                                    }
                                    catch
                                    {
                                    }
                                }
                            }

                            db.tbloperatordetails.Add(tblmp);
                            try
                            {
                                db.SaveChanges();
                            }
                            catch (DbEntityValidationException e)
                            {
                                foreach (var eve in e.EntityValidationErrors)
                                {
                                    Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                                      eve.Entry.Entity.GetType().Name, eve.Entry.State);
                                    foreach (var ve in eve.ValidationErrors)
                                    {
                                        Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                                                          ve.PropertyName, ve.ErrorMessage);
                                    }
                                }
                                throw;
                            }
                        }
                        else
                        {
                            using (i_facility_shaktiEntities1 db1 = new i_facility_shaktiEntities1())
                            {
                                if (string.IsNullOrEmpty(Dept) || string.IsNullOrEmpty(OperatorName) || OperatorID == null)
                                {
                                    text = text + htmlerrorMaker(Dept, OperatorName, OperatorID, "Dept, OperatorName or OperatorID cannot be empty/Check the format");
                                    continue;
                                }
                                else
                                {
                                    var OPTDet = db.tbloperatordetails.Where(m => m.isDeleted == 0 && m.OperatorID == OperatorID).FirstOrDefault();
                                    OPTDet.isDeleted       = 1;
                                    db.Entry(OPTDet).State = System.Data.Entity.EntityState.Modified;
                                    db.SaveChanges();
                                    try
                                    {
                                        tblmp.Dept         = Dept.Trim();
                                        tblmp.OperatorName = OperatorName.Trim();
                                        tblmp.OperatorID   = OperatorID;
                                        tblmp.CreatedBy    = Convert.ToInt32(UserId);
                                        tblmp.CreatedOn    = DateTime.Now;
                                    }
                                    catch
                                    {
                                    }
                                }
                            }

                            db.tbloperatordetails.Add(tblmp);
                            try
                            {
                                db.SaveChanges();
                            }
                            catch (DbEntityValidationException e)
                            {
                                foreach (var eve in e.EntityValidationErrors)
                                {
                                    Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                                      eve.Entry.Entity.GetType().Name, eve.Entry.State);
                                    foreach (var ve in eve.ValidationErrors)
                                    {
                                        Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                                                          ve.PropertyName, ve.ErrorMessage);
                                    }
                                }
                                throw;
                            }
                        }
                    }
                    #endregion
                }
                else if (UploadType == "OverWrite") // Delete Duplicate and Insert New. // if not Duplicate insert that
                {
                    #region
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        tbloperatordetail tblmp    = new tbloperatordetail();
                        String            Username = Session["Username"].ToString();
                        tblmp.CreatedBy = Convert.ToInt32(Session["UserId"]);
                        tblmp.CreatedOn = DateTime.Now;
                        tblmp.isDeleted = 0;

                        string Dept = null, OperatorName = null;
                        string OperatorID = null;
                        Dept         = Convert.ToString(ds.Tables[0].Rows[i][0]);
                        OperatorName = Convert.ToString(ds.Tables[0].Rows[i][1]);
                        OperatorID   = Convert.ToString(ds.Tables[0].Rows[i][2]);

                        bool check = ValidationCheckForInsertion(OperatorID);
                        if (check == true)
                        {
                            using (i_facility_shaktiEntities1 db1 = new i_facility_shaktiEntities1())
                            {
                                if (string.IsNullOrEmpty(Dept) || string.IsNullOrEmpty(OperatorName) || OperatorID == null)
                                {
                                    text = text + htmlerrorMaker(Dept, OperatorName, OperatorID, "Dept, OperatorName or OperatorID cannot be empty/Check the format");
                                    continue;
                                }
                                else
                                {
                                    try
                                    {
                                        tblmp.Dept         = Dept.Trim();
                                        tblmp.OperatorName = OperatorName.Trim();
                                        tblmp.OperatorID   = OperatorID;
                                        tblmp.CreatedBy    = Convert.ToInt32(UserId);
                                        tblmp.CreatedOn    = DateTime.Now;
                                    }
                                    catch
                                    {
                                    }
                                }
                            }

                            db.tbloperatordetails.Add(tblmp);
                            try
                            {
                                db.SaveChanges();
                            }
                            catch (DbEntityValidationException e)
                            {
                                foreach (var eve in e.EntityValidationErrors)
                                {
                                    Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                                      eve.Entry.Entity.GetType().Name, eve.Entry.State);
                                    foreach (var ve in eve.ValidationErrors)
                                    {
                                        Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                                                          ve.PropertyName, ve.ErrorMessage);
                                    }
                                }
                                throw;
                            }
                        }
                        else
                        {
                            var OPTDet = db.tbloperatordetails.Where(m => m.isDeleted == 0 && m.OperatorID == OperatorID).FirstOrDefault();
                            using (i_facility_shaktiEntities1 db1 = new i_facility_shaktiEntities1())
                            {
                                if (string.IsNullOrEmpty(Dept) || string.IsNullOrEmpty(OperatorName) || OperatorID == null)
                                {
                                    text = text + htmlerrorMaker(Dept, OperatorName, OperatorID, "Dept, OperatorName or OperatorID cannot be empty/Check the format");
                                    continue;
                                }
                                else
                                {
                                    try
                                    {
                                        OPTDet.Dept            = Dept.Trim();
                                        OPTDet.OperatorName    = OperatorName.Trim();
                                        OPTDet.ModifiedBy      = Convert.ToInt32(UserId);
                                        OPTDet.ModifiedOn      = DateTime.Now;
                                        db.Entry(OPTDet).State = System.Data.Entity.EntityState.Modified;
                                    }
                                    catch
                                    {
                                    }
                                }
                            }

                            //db.tbloperatordetails.Add(OPTDet);
                            try
                            {
                                db.SaveChanges();
                            }
                            catch (DbEntityValidationException e)
                            {
                                foreach (var eve in e.EntityValidationErrors)
                                {
                                    Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                                      eve.Entry.Entity.GetType().Name, eve.Entry.State);
                                    foreach (var ve in eve.ValidationErrors)
                                    {
                                        Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                                                          ve.PropertyName, ve.ErrorMessage);
                                    }
                                }
                                throw;
                            }
                        }
                    }
                    #endregion
                }

                //TempData["txtShow"] = text;
                //// ViewBag.text = text;
                //Session["PartNo"] = ErrorMsg;
            }
            //return RedirectToAction("Index", "MasterParts");
            return(RedirectToAction("Index", "OperatorModule"));
        }