///<Summary>
        ///Select all rows by foreign key
        ///This method returns all data rows in the table Territories based on a foreign key
        ///</Summary>
        ///<returns>
        ///IList-IDAOTerritories.
        ///</returns>
        ///<parameters>
        ///Int32? regionID
        ///</parameters>
        public static IList <IDAOTerritories> SelectAllByRegionID(Int32?regionID)
        {
            Doing(null);
            SqlCommand command = new SqlCommand();

            command.CommandText = InlineProcs.ctprTerritories_SelectAllByRegionID;
            command.CommandType = CommandType.Text;
            SqlConnection staticConnection = StaticSqlConnection;

            command.Connection = staticConnection;

            DataTable      dt         = new DataTable("Territories");
            SqlDataAdapter sqlAdapter = new SqlDataAdapter(command);

            try
            {
                command.Parameters.Add(CtSqlParameter.Get("@RegionID", SqlDbType.Int, 4, ParameterDirection.Input, false, 10, 0, "", DataRowVersion.Proposed, (object)regionID ?? (object)DBNull.Value));

                staticConnection.Open();
                sqlAdapter.Fill(dt);
                Done(null);

                List <IDAOTerritories> objList = new List <IDAOTerritories>();
                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow row in dt.Rows)
                    {
                        DAOTerritories retObj = new DAOTerritories();
                        retObj._territoryID          = Convert.IsDBNull(row["TerritoryID"]) ? null : (string)row["TerritoryID"];
                        retObj._territoryDescription = Convert.IsDBNull(row["TerritoryDescription"]) ? null : (string)row["TerritoryDescription"];
                        retObj._regionID             = Convert.IsDBNull(row["RegionID"]) ? (Int32?)null : (Int32?)row["RegionID"];
                        retObj._ctrVersion           = Convert.IsDBNull(row["ctr_version"]) ? (Int32?)null : (Int32?)row["ctr_version"];
                        objList.Add(retObj);
                    }
                }
                return(objList);
            }
            catch (Exception ex)
            {
                Failed(null, ex);
                Handle(null, ex);
                return(null);
            }
            finally
            {
                staticConnection.Close();
                command.Dispose();
            }
        }
        ///<Summary>
        ///Select all rows by filter criteria
        ///This method returns all data rows in the table using criteriaquery api Territories
        ///</Summary>
        ///<returns>
        ///IList-IDAOTerritories.
        ///</returns>
        ///<parameters>
        ///IList<IDataCriterion> listCriterion, IList<IDataOrderBy> listOrder, IDataSkip dataSkip, IDataTake dataTake
        ///</parameters>
        public static IList <IDAOTerritories> SelectAllByCriteria(IList <IDataCriterion> listCriterion, IList <IDataOrderBy> listOrder, IDataSkip dataSkip, IDataTake dataTake)
        {
            Doing(null);
            SqlCommand command = new SqlCommand();

            command.CommandText = GetSelectionCriteria(InlineProcs.ctprTerritories_SelectAllByCriteria, null, listCriterion, listOrder, dataSkip, dataTake);
            command.CommandType = CommandType.Text;
            SqlConnection staticConnection = StaticSqlConnection;

            command.Connection = staticConnection;

            DataTable      dt         = new DataTable("Territories");
            SqlDataAdapter sqlAdapter = new SqlDataAdapter(command);

            try
            {
                staticConnection.Open();
                sqlAdapter.Fill(dt);
                Done(null);

                List <IDAOTerritories> objList = new List <IDAOTerritories>();
                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow row in dt.Rows)
                    {
                        DAOTerritories retObj = new DAOTerritories();
                        retObj._territoryID          = Convert.IsDBNull(row["TerritoryID"]) ? null : (string)row["TerritoryID"];
                        retObj._territoryDescription = Convert.IsDBNull(row["TerritoryDescription"]) ? null : (string)row["TerritoryDescription"];
                        retObj._regionID             = Convert.IsDBNull(row["RegionID"]) ? (Int32?)null : (Int32?)row["RegionID"];
                        retObj._ctrVersion           = Convert.IsDBNull(row["ctr_version"]) ? (Int32?)null : (Int32?)row["ctr_version"];
                        objList.Add(retObj);
                    }
                }
                return(objList);
            }
            catch (Exception ex)
            {
                Failed(null, ex);
                Handle(null, ex);
                return(null);
            }
            finally
            {
                staticConnection.Close();
                command.Dispose();
            }
        }
        ///<Summary>
        ///Select one row by primary key(s)
        ///This method returns one row from the table Territories based on the primary key(s)
        ///</Summary>
        ///<returns>
        ///IDAOTerritories
        ///</returns>
        ///<parameters>
        ///string territoryID
        ///</parameters>
        public static IDAOTerritories SelectOne(string territoryID)
        {
            Doing(null);
            SqlCommand command = new SqlCommand();

            command.CommandText = InlineProcs.ctprTerritories_SelectOne;
            command.CommandType = CommandType.Text;
            SqlConnection staticConnection = StaticSqlConnection;

            command.Connection = staticConnection;

            DataTable      dt         = new DataTable("Territories");
            SqlDataAdapter sqlAdapter = new SqlDataAdapter(command);

            try
            {
                command.Parameters.Add(CtSqlParameter.Get("@TerritoryID", SqlDbType.NVarChar, 20, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, (object)territoryID ?? (object)DBNull.Value));

                staticConnection.Open();
                sqlAdapter.Fill(dt);
                Done(null);

                DAOTerritories retObj = null;
                if (dt.Rows.Count > 0)
                {
                    retObj = new DAOTerritories();
                    retObj._territoryID          = Convert.IsDBNull(dt.Rows[0]["TerritoryID"]) ? null : (string)dt.Rows[0]["TerritoryID"];
                    retObj._territoryDescription = Convert.IsDBNull(dt.Rows[0]["TerritoryDescription"]) ? null : (string)dt.Rows[0]["TerritoryDescription"];
                    retObj._regionID             = Convert.IsDBNull(dt.Rows[0]["RegionID"]) ? (Int32?)null : (Int32?)dt.Rows[0]["RegionID"];
                    retObj._ctrVersion           = Convert.IsDBNull(dt.Rows[0]["ctr_version"]) ? (Int32?)null : (Int32?)dt.Rows[0]["ctr_version"];
                }
                return(retObj);
            }
            catch (Exception ex)
            {
                Failed(null, ex);
                Handle(null, ex);
                return(null);
            }
            finally
            {
                staticConnection.Close();
                command.Dispose();
            }
        }