//END - create

        //BEGIN - update
        public void UpdateEmployeeterritories(EmployeeterritoriesSingle employeeterritories)
        {
            try     // handle exogenous exceptions
            {
                try // log all exceptions
                {
                    using (SqlConnection con = new SqlConnection(connectionString))
                    {
                        using (SqlCommand cmd = new SqlCommand("spUpdateEmployeeterritories", con)
                        {
                            CommandType = CommandType.StoredProcedure
                        })
                        {
                            cmd.Parameters.AddWithValue("@EmployeeID", employeeterritories.EmployeeID);
                            cmd.Parameters.AddWithValue("@TerritoryID", employeeterritories.TerritoryID);
                            con.Open();
                            cmd.ExecuteNonQuery();
                        }
                        con.Close();
                    }
                }
                catch (Exception ex)
                {
                    BusinessLayer.ExceptionLogging exlog = new BusinessLayer.ExceptionLogging();
                    exlog.SendExcepToDB(ex);
                    //errResult = "A Technical Error occurred, Please visit after some time.";
                    throw;
                }
            }
            catch (Exception fx)
            {
                errResult = fx.Message.ToString();
                throw;
            }
        }
        //END - delete


        //BEGIN - read
        public List <EmployeeterritoriesSingle> GetAllEmployeeterritoriess()
        {
            try     // handle exogenous exceptions
            {
                try // log all exceptions
                {
                    List <EmployeeterritoriesSingle> employeeterritoriess = new List <EmployeeterritoriesSingle>();

                    using (SqlConnection con = new SqlConnection(connectionString))
                    {
                        SqlCommand cmd = new SqlCommand("spGetAllEmployeeterritories", con)
                        {
                            CommandType = CommandType.StoredProcedure
                        };

                        con.Open();

                        SqlDataReader rdr = cmd.ExecuteReader();

                        while (rdr.Read())
                        {
                            EmployeeterritoriesSingle employeeterritories = new EmployeeterritoriesSingle
                            {
                                // EXAMPLES:
                                //EmployeeId = Convert.ToInt32(rdr["EmployeeId"]),
                                //Name = rdr["Name"].ToString(),
                                //IsPermanent = (bool)rdr["IsPermanent"],
                                //Salary = Convert.ToDecimal(rdr["Salary"]),
                                //DateOfBirth = Convert.ToDateTime(rdr["DateOfBirth"])

                                //EmployeeID = (int)rdr["EmployeeID"]
                                EmployeeID = rdr["EmployeeID"] == DBNull.Value ? default(int) : (int)rdr["EmployeeID"]
                                             //,TerritoryID = (string)rdr["TerritoryID"]
                                ,
                                TerritoryID = rdr["TerritoryID"] == DBNull.Value ? "" : (string)rdr["TerritoryID"]
                            };
                            employeeterritoriess.Add(employeeterritories);
                        }
                        con.Close();
                        cmd.Dispose();
                    }
                    return(employeeterritoriess);
                }
                catch (Exception ex)
                {
                    BusinessLayer.ExceptionLogging exlog = new BusinessLayer.ExceptionLogging();
                    exlog.SendExcepToDB(ex);
                    //errResult = "A Technical Error occurred, Please visit after some time.";
                    throw;
                }
            }
            catch (Exception fx)
            {
                errResult = fx.Message.ToString();
                throw;
            }
        }
        //BEGIN - readBy
        public EmployeeterritoriesSingle GetEmployeeterritoriesData(int EmployeeID)
        {
            try     // handle exogenous exceptions
            {
                try // log all exceptions
                {
                    EmployeeterritoriesSingle employeeterritories = new EmployeeterritoriesSingle();
                    using (SqlConnection con = new SqlConnection(connectionString))
                    {
                        string sqlQuery = "SELECT * FROM [EmployeeTerritories] WHERE EmployeeID= " + EmployeeID.ToString();

                        using (SqlCommand cmd = new SqlCommand(sqlQuery, con))
                        {
                            con.Open();
                            SqlDataReader rdr = cmd.ExecuteReader();
                            while (rdr.Read())
                            {
                                //employeeterritories.EmployeeID = (int)rdr["EmployeeID"];
                                employeeterritories.EmployeeID = rdr["EmployeeID"] == DBNull.Value ? default(int) : (int)rdr["EmployeeID"];
                                //employeeterritories.TerritoryID = (string)rdr["TerritoryID"];
                                employeeterritories.TerritoryID = rdr["TerritoryID"] == DBNull.Value ? "" : (string)rdr["TerritoryID"];


                                //EXAMPLES:
                                //employee.EmployeeId = Convert.ToInt32(rdr["EmployeeID"]);
                                //employee.Name = rdr["Name"].ToString();
                                //employee.Gender = rdr["Gender"].ToString();
                                //employee.Salary = (decimal)rdr["Salary"];
                                //employee.City = rdr["City"].ToString();
                                //employee.IsPermanent = (bool)rdr["IsPermanent"];
                                //employee.DateOfBirth = Convert.ToDateTime(rdr["DateOfBirth"]);
                            }
                        }
                    }
                    return(employeeterritories);
                }
                catch (Exception ex)
                {
                    BusinessLayer.ExceptionLogging exlog = new BusinessLayer.ExceptionLogging();
                    exlog.SendExcepToDB(ex);
                    //errResult = "A Technical Error occurred, Please visit after some time.";
                    throw;
                }
            }
            catch (Exception fx)
            {
                errResult = fx.Message.ToString();
                throw;
            }
        }
        public ActionResult Create_Post()
        {
            try     // handle exogenous exceptions
            {
                try // log all exceptions
                {
                    EmployeesBusinessModelLayers employeesLUBusinessModelLayers = new EmployeesBusinessModelLayers();
                    ViewBag.Employeess = new SelectList(employeesLUBusinessModelLayers.EmployeesSelect, "EmployeeID", "LastName");
                    TerritoriesBusinessModelLayers territoriesLUBusinessModelLayers = new TerritoriesBusinessModelLayers();
                    ViewBag.Territoriess = new SelectList(territoriesLUBusinessModelLayers.TerritoriesSelect, "TerritoryID", "TerritoryDescription");

                    EmployeeterritoriesBusinessModelLayers       employeeterritoriesBusinessModelLayers = new EmployeeterritoriesBusinessModelLayers();
                    BusinessModelLayer.EmployeeterritoriesSingle employeeterritories = new BusinessModelLayer.EmployeeterritoriesSingle();
                    TryUpdateModel(employeeterritories);
                    if (ModelState.IsValid)
                    {
                        //mm
                        employeeterritoriesBusinessModelLayers.AddEmployeeterritories(employeeterritories);
                        return(RedirectToAction("List"));
                    }
                    else
                    {
                        return(View());
                    }
                }
                catch (Exception ex)
                {
                    BusinessLayer.ExceptionLogging exlog = new BusinessLayer.ExceptionLogging();
                    exlog.SendExcepToDB(ex);
                    throw;
                }
            }
            catch (Exception)
            {
                throw;
            }
        }