public ActionResult Create_Post()
        {
            try     // handle exogenous exceptions
            {
                try // log all exceptions
                {
                    RegionBusinessModelLayers regionLUBusinessModelLayers = new RegionBusinessModelLayers();
                    ViewBag.Regions = new SelectList(regionLUBusinessModelLayers.RegionSelect, "RegionID", "RegionDescription");

                    TerritoriesBusinessModelLayers       territoriesBusinessModelLayers = new TerritoriesBusinessModelLayers();
                    BusinessModelLayer.TerritoriesSingle territories = new BusinessModelLayer.TerritoriesSingle();
                    TryUpdateModel(territories);
                    if (ModelState.IsValid)
                    {
                        //mm
                        territoriesBusinessModelLayers.AddTerritories(territories);
                        return(RedirectToAction("List"));
                    }
                    else
                    {
                        return(View());
                    }
                }
                catch (Exception ex)
                {
                    BusinessLayer.ExceptionLogging exlog = new BusinessLayer.ExceptionLogging();
                    exlog.SendExcepToDB(ex);
                    throw;
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
        public ActionResult Details(string TerritoryID)
        {
            try     // handle exogenous exceptions
            {
                try // log all exceptions
                {
                    RegionBusinessModelLayers regionLUBusinessModelLayers = new RegionBusinessModelLayers();
                    ViewBag.Regions = new SelectList(regionLUBusinessModelLayers.RegionSelect, "RegionID", "RegionDescription");

                    TerritoriesBusinessModelLayers territoriesBusinessModelLayers = new TerritoriesBusinessModelLayers();

                    BusinessModelLayer.TerritoriesSingle territories = territoriesBusinessModelLayers.GetAllTerritoriess().FirstOrDefault(x => x.TerritoryID == TerritoryID);

                    return(View(territories));
                }
                catch (Exception ex)
                {
                    BusinessLayer.ExceptionLogging exlog = new BusinessLayer.ExceptionLogging();
                    exlog.SendExcepToDB(ex);
                    throw;
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
        //END - delete


        //BEGIN - read
        public List <TerritoriesSingle> GetAllTerritoriess()
        {
            try     // handle exogenous exceptions
            {
                try // log all exceptions
                {
                    List <TerritoriesSingle> territoriess = new List <TerritoriesSingle>();

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

                        con.Open();

                        SqlDataReader rdr = cmd.ExecuteReader();

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

                                //TerritoryID = (string)rdr["TerritoryID"]
                                TerritoryID = rdr["TerritoryID"] == DBNull.Value ? "" : (string)rdr["TerritoryID"]
                                              //,TerritoryDescription = (string)rdr["TerritoryDescription"]
                                ,
                                TerritoryDescription = rdr["TerritoryDescription"] == DBNull.Value ? "" : (string)rdr["TerritoryDescription"]
                                                       //,RegionID = (int)rdr["RegionID"]
                                ,
                                RegionID = rdr["RegionID"] == DBNull.Value ? default(int) : (int)rdr["RegionID"]
                            };
                            territoriess.Add(territories);
                        }
                        con.Close();
                        cmd.Dispose();
                    }
                    return(territoriess);
                }
                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 TerritoriesSingle GetTerritoriesData(string TerritoryID)
        {
            try     // handle exogenous exceptions
            {
                try // log all exceptions
                {
                    TerritoriesSingle territories = new TerritoriesSingle();
                    using (SqlConnection con = new SqlConnection(connectionString))
                    {
                        string sqlQuery = "SELECT * FROM [Territories] WHERE TerritoryID= '" + TerritoryID.ToString() + "'";

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

                                //territories.TerritoryDescription = (string)rdr["TerritoryDescription"];
                                territories.TerritoryDescription = rdr["TerritoryDescription"] == DBNull.Value ? "" : (string)rdr["TerritoryDescription"];


                                //territories.RegionID = (int)rdr["RegionID"];
                                territories.RegionID = rdr["RegionID"] == DBNull.Value ? default(int) : (int)rdr["RegionID"];

                                //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(territories);
                }
                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 - readBy
        //BEGIN - create
        public void AddTerritories(TerritoriesSingle territories)
        {
            try     // handle exogenous exceptions
            {
                try // log all exceptions
                {
                    using (SqlConnection con = new SqlConnection(connectionString))
                    {
                        SqlCommand cmd = new SqlCommand("spAddTerritories", con)
                        {
                            CommandType = CommandType.StoredProcedure
                        };
                        SqlParameter paramTerritoryID = new SqlParameter
                        {
                            ParameterName = "@TerritoryID",
                            Value         = territories.TerritoryID
                        };
                        cmd.Parameters.Add(paramTerritoryID);

                        SqlParameter paramTerritoryDescription = new SqlParameter
                        {
                            ParameterName = "@TerritoryDescription",
                            Value         = territories.TerritoryDescription
                        };
                        cmd.Parameters.Add(paramTerritoryDescription);

                        SqlParameter paramRegionID = new SqlParameter
                        {
                            ParameterName = "@RegionID",
                            Value         = territories.RegionID
                        };
                        cmd.Parameters.Add(paramRegionID);

                        con.Open();
                        cmd.ExecuteNonQuery();
                        cmd.Dispose();
                    }
                }
                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;
            }
        }