public void Delete(Location deleteThis)
        {
            _dataEngine.InitialiseParameterList();
            _dataEngine.AddParameter("@Id", deleteThis.Id.ToString());

            _sqlToExecute = "DELETE FROM [dbo].[Location] WHERE Id = " + _dataEngine.GetParametersForQuery();

            if (!_dataEngine.ExecuteSql(_sqlToExecute))
                throw new Exception("Location - Delete failed");
        }
 public int Insert(Location saveThis)
 {
     try
        {
        _db.Locations.InsertOnSubmit(saveThis);
        _db.SubmitChanges();
        return saveThis.Id;
        }
        catch (Exception e)
        {
        throw new Exception(e.Message);
        }
 }
 public void Delete(Location deleteThis)
 {
     try
     {
         if (deleteThis.Id > 0)
         {
             _db.Locations.DeleteOnSubmit(deleteThis);
             _db.SubmitChanges(ConflictMode.FailOnFirstConflict);
         }
     }
     catch (Exception e)
     {
         throw new Exception(e.Message);
     }
 }
 public void Update(Location updateThis)
 {
     try
     {
         _db.Locations.Attach(updateThis);
         _db.Refresh(RefreshMode.KeepCurrentValues, updateThis);
         _db.SubmitChanges();
     }
     catch (Exception e)
     {
         throw new Exception(e.Message);
     }
 }
        public void CreateLocation(int customerId, ref Location location, ref Address address)
        {
            //Check a valid customer id has been passed
            try
            {
                var customer = _da.Value.Customer.GetById(customerId);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            //Create a new location record
            var insertedRowId = _da.Value.Location.Insert(location);

            if (insertedRowId == 0)
            {
                throw new Exception("Failed to create location record");
            }
            else
            {
                location.Id = insertedRowId;
            }

            //Link the customer and location
            var customerLocationLink = new LinkObjectMaster()
            {
                MasterLinkId = customerId,
                MasterLinkType = LinkType.Customer,
                ChildLinkId = location.Id,
                ChildLinkType = LinkType.Location
            };

            //Save the link record
            insertedRowId = _da.Value.Link.Insert(customerLocationLink);

            if (insertedRowId == 0)
            {
                //Roll back the inserts as it's failed
                //Delete the location record
                _da.Value.Location.Delete(location);

                throw new Exception("Failed to create customer location link record, transaction rolled back");
            }

            //Save the address record
            var addressCreated = false;
            if (address.Id == 0)
            {
                //Create a new address record
                insertedRowId = _da.Value.Address.Insert(address);

                if (insertedRowId == 0)
                {
                    throw new Exception("Failed to create address record");
                }
                else
                {
                    address.Id = insertedRowId;
                    addressCreated = true;
                }
            }

            //Link the location and address records
            var locationAddressLink = new LinkObjectMaster()
            {
                MasterLinkId = location.Id,
                MasterLinkType = LinkType.Location,
                ChildLinkId = address.Id,
                ChildLinkType = LinkType.Address
            };

            //Save the link record
            insertedRowId = _da.Value.Link.Insert(locationAddressLink);

            if (insertedRowId == 0)
            {
                //Roll back the inserts as it's failed
                //Delete the location record
                _da.Value.Location.Delete(location);

                if (addressCreated)
                    _da.Value.Address.Delete(address);

                throw new Exception("Failed to create location address link record, transaction rolled back");
            }
        }
 public void UpdateLocation(ref Location location)
 {
     _da.Value.Location.Update(location);
 }
        public int Insert(Location saveThis)
        {
            _dataEngine.InitialiseParameterList();
            _dataEngine.AddParameter("@LocationDescription", saveThis.LocationDescription);

            _sqlToExecute = "INSERT INTO [dbo].[Location] ";
            _sqlToExecute += "([LocationDescription]) ";
            _sqlToExecute += "OUTPUT INSERTED.Id ";
            _sqlToExecute += "VALUES ";
            _sqlToExecute += "(";
            _sqlToExecute += _dataEngine.GetParametersForQuery();
            _sqlToExecute += ")";

            int insertedRowId = 0;

            if (!_dataEngine.ExecuteSql(_sqlToExecute, out insertedRowId))
                throw new Exception("Location - Save failed");

            return insertedRowId;
        }
        /// <summary>
        /// Creates the object from the data returned from the database
        /// </summary>
        /// <returns></returns>
        private Location CreateLocationFromData()
        {
            var location = new Location
            {
                Id = int.Parse(_dataEngine.Dr["Id"].ToString()),
                LocationDescription = _dataEngine.Dr["LocationDescription"].ToString()
            };

            return location;
        }
        public void Update(Location saveThis)
        {
            _dataEngine.InitialiseParameterList();
            _dataEngine.AddParameter("@LocationDescription", saveThis.LocationDescription);

            _sqlToExecute = "UPDATE [dbo].[Location] SET ";
            _sqlToExecute += "[LocationDescription] = @LocationDescription ";
            _sqlToExecute += "WHERE [Id] = " + saveThis.Id;

            if (!_dataEngine.ExecuteSql(_sqlToExecute))
                throw new Exception("Location - Update failed");
        }