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

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

            if (!_dataEngine.ExecuteSql(_sqlToExecute))
                throw new Exception("Address - Delete failed");
        }
        public void CreateCustomer(ref Customer customer, ref Address address)
        {
            //Save the customer record
            var insertedRowId = _da.Value.Customer.Insert(customer);

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

            //Save the address record
            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;
                }
            }

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

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

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

                //Delete the address record
                _da.Value.Address.Delete(address);

                throw new Exception("Failed to create customer address link record, transaction rolled back");
            }
        }
 public int Insert(Address saveThis)
 {
     try
        {
        _db.Addresses.InsertOnSubmit(saveThis);
        _db.SubmitChanges();
        return saveThis.Id;
        }
        catch (Exception e)
        {
        throw new Exception(e.Message);
        }
 }
 public void Delete(Address deleteThis)
 {
     try
     {
         if (deleteThis.Id > 0)
         {
             _db.Addresses.DeleteOnSubmit(deleteThis);
             _db.SubmitChanges(ConflictMode.FailOnFirstConflict);
         }
     }
     catch (Exception e)
     {
         throw new Exception(e.Message);
     }
 }
 public void Update(Address updateThis)
 {
     try
     {
         _db.Addresses.Attach(updateThis);
         _db.Refresh(RefreshMode.KeepCurrentValues, updateThis);
         _db.SubmitChanges();
     }
     catch (Exception e)
     {
         throw new Exception(e.Message);
     }
 }
        public int Insert(Address saveThis)
        {
            _dataEngine.InitialiseParameterList();
            _dataEngine.AddParameter("@Address1", saveThis.Address1);
            _dataEngine.AddParameter("@Address2", saveThis.Address2);
            _dataEngine.AddParameter("@Address3", saveThis.Address3);
            _dataEngine.AddParameter("@Town", saveThis.Town);
            _dataEngine.AddParameter("@County", saveThis.County);
            _dataEngine.AddParameter("@Country", saveThis.Country);
            _dataEngine.AddParameter("@AddressOther", saveThis.AddressOther);
            _dataEngine.AddParameter("@PostCode", saveThis.PostCode);

            _sqlToExecute = "INSERT INTO [dbo].[Address] ";
               _sqlToExecute += "([Address1]";
               _sqlToExecute += ",[Address2]";
               _sqlToExecute += ",[Address3]";
               _sqlToExecute += ",[Town]";
               _sqlToExecute += ",[County]";
               _sqlToExecute += ",[Country]";
               _sqlToExecute += ",[AddressOther]";
               _sqlToExecute += ",[PostCode]) ";
               _sqlToExecute += "OUTPUT INSERTED.Id ";
               _sqlToExecute += "VALUES ";
               _sqlToExecute += "(";
               _sqlToExecute += _dataEngine.GetParametersForQuery();
               _sqlToExecute += ")";

               int insertedRowId = 0;

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

               return insertedRowId;
        }
        /// <summary>
        /// Creates the object from the data returned from the database
        /// </summary>
        /// <returns></returns>
        private Address CreateAddressFromData()
        {
            var address = new Address
            {
                Address1 = _dataEngine.Dr["Address1"].ToString(),
                Address2 = _dataEngine.Dr["Address2"].ToString(),
                Address3 = _dataEngine.Dr["Address3"].ToString(),
                Town = _dataEngine.Dr["Town"].ToString(),
                County = _dataEngine.Dr["County"].ToString(),
                Country = _dataEngine.Dr["Country"].ToString(),
                AddressOther = _dataEngine.Dr["AddressOther"].ToString(),
                PostCode = _dataEngine.Dr["PostCode"].ToString(),
                Id = int.Parse(_dataEngine.Dr["Id"].ToString())
            };

            return address;
        }
        public void Update(Address saveThis)
        {
            _dataEngine.InitialiseParameterList();
            _dataEngine.AddParameter("@Address1", saveThis.Address1);
            _dataEngine.AddParameter("@Address2", saveThis.Address2);
            _dataEngine.AddParameter("@Address3", saveThis.Address3);
            _dataEngine.AddParameter("@Town", saveThis.Town);
            _dataEngine.AddParameter("@County", saveThis.County);
            _dataEngine.AddParameter("@Country", saveThis.Country);
            _dataEngine.AddParameter("@AddressOther", saveThis.AddressOther);
            _dataEngine.AddParameter("@PostCode", saveThis.PostCode);

            _sqlToExecute = "UPDATE [dbo].[Address] SET ";
            _sqlToExecute += "[Address1] = @Address1";
            _sqlToExecute += ",[Address2] = @Address2";
            _sqlToExecute += ",[Address3] = @Address3";
            _sqlToExecute += ",[Town] = @Town";
            _sqlToExecute += ",[County] = @County";
            _sqlToExecute += ",[Country] = @Country";
            _sqlToExecute += ",[AddressOther] = @AddressOther";
            _sqlToExecute += ",[PostCode] = PostCode ";
            _sqlToExecute += "WHERE [Id] = " + saveThis.Id;

            if (!_dataEngine.ExecuteSql(_sqlToExecute))
                throw new Exception("Address - Update failed");
        }
        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");
            }
        }