private void SaveOrganisationLookupDetail(LookupOrganisationalUnitDataSet.LookupOrganisationalUnitDataTable dtLookupOrganisationalUnitDataTable,  LookupOrganisationalUnitDataSet.LookupOrganisationalUnitDataTable dtOrgDetail, int? AddressID)
        {
            string transactionid = NCS.IConnect.ApplicationContexts.ApplicationContextFactory.GetApplicationContext().TransactionId;
            DbHelper helper = new DbHelper();
            DbCommand command  = null;

            if (dtOrgDetail.Rows.Count > 0)
                command = helper.BuildDbCommand("P_SS_OrganisationalUnitDetail_U");
            else
                command = helper.BuildDbCommand("P_SS_OrganisationalUnitDetail_I");

            helper.AssignParameterValues(command,
                Membership.ApplicationName,
                dtLookupOrganisationalUnitDataTable[0].OrganisationalUnitID,
                AddressID,
                dtLookupOrganisationalUnitDataTable[0].Phone,
                dtLookupOrganisationalUnitDataTable[0].Fax,
                dtLookupOrganisationalUnitDataTable[0].Mobile,
                dtLookupOrganisationalUnitDataTable[0].TollFreePhone,
                dtLookupOrganisationalUnitDataTable[0].Email,
                1,
                transactionid,
                AppContext.Current.UserName.ToString(),
                DateTime.Now);

            Helper.ExecuteNonQuery(command);
        }
        private int? SaveAddress(LookupOrganisationalUnitDataSet.LookupOrganisationalUnitDataTable dtLookupOrganisationalUnitDataTable, LookupOrganisationalUnitDataSet.LookupOrganisationalUnitDataTable dtOrgDetail)
        {
            int? retaddressID = null;
            int versionAddressId = 1;

            var row = dtLookupOrganisationalUnitDataTable[0];
            if (string.IsNullOrEmpty(row.State)
                && string.IsNullOrEmpty(row.UnitNumber)
                && string.IsNullOrEmpty(row.StreetNumber)
                && string.IsNullOrEmpty(row.Street)
                && string.IsNullOrEmpty(row.Type)
                && string.IsNullOrEmpty(row.Suffix)
                && string.IsNullOrEmpty(row.Suburb)
                && string.IsNullOrEmpty(row.PostCode))
            {
                return retaddressID;
            }

            //If need to add address, State cannot be empty
            if (string.IsNullOrEmpty(row.State))
            {
                throw new ArgumentException("State is mandatory when to add address.");
            }

            string address1 = string.Concat(row.UnitNumber,
                                            string.IsNullOrEmpty(row.UnitNumber) &&
                                            string.IsNullOrEmpty(row.StreetNumber)
                                                ? string.Empty
                                                : "/", row.StreetNumber, " ",
                                            row.Street, " ",
                                            row.Type).Trim();

            if(string.IsNullOrEmpty(address1) == false)
            {
                if(address1.EndsWith("/"))
                {
                    address1 = address1.Substring(0, address1.Length - 1);
                }

                if(address1.StartsWith("/"))
                {
                    address1 = address1.Substring(1, address1.Length - 1);
                }

            }

            if (dtOrgDetail.Rows.Count > 0)
            {
                if (string.IsNullOrEmpty(dtOrgDetail[0].AddressID))
                {
                    retaddressID = IsAddressExist(dtLookupOrganisationalUnitDataTable, address1, out versionAddressId);
                }
                else
                {
                    versionAddressId = GetAddressVersion(Convert.ToInt32(dtOrgDetail[0].AddressID));
                    retaddressID = Convert.ToInt32(dtOrgDetail[0].AddressID.ToString());
                }
            }
            else
                retaddressID = IsAddressExist(dtLookupOrganisationalUnitDataTable, address1, out versionAddressId);

            if (retaddressID == null)
            {
                string transactionid =
                    NCS.IConnect.ApplicationContexts.ApplicationContextFactory.GetApplicationContext().TransactionId;
                DbHelper helper = new DbHelper();
                DbCommand command = helper.BuildDbCommand("P_CM_Address_I");

                helper.AssignParameterValues(command,
                                             null,
                                             null,
                                             string.IsNullOrEmpty(row.UnitNumber)?null:row.UnitNumber,
                                             null,
                                             null,
                                             string.IsNullOrEmpty(row.StreetNumber) ? null : row.StreetNumber,
                                             null,
                                             string.IsNullOrEmpty(row.Street) ? null : row.Street,
                                             string.IsNullOrEmpty(row.Type) ? null : row.Type,
                                             string.IsNullOrEmpty(row.Suffix) ? null : row.Suffix,
                                             null,
                                             null,
                                             string.IsNullOrEmpty(row.Suburb) ? null : row.Suburb,
                                             string.IsNullOrEmpty(row.State) ? null : row.State,
                                             string.IsNullOrEmpty(row.PostCode) ? null : row.PostCode,
                                             string.IsNullOrEmpty(address1.Trim()) ? null : address1.Trim(),
                                             null,
                                             null,
                                             null,
                                             null,
                                             null,
                                             0,
                                             null,
                                             null,
                                             null,
                                             null,
                                             1101,
                                             null,
                                             0,
                                             versionAddressId,
                                             transactionid,
                                             AppContext.Current.UserName.ToString(),
                                             DateTime.Now,
                                             AppContext.Current.UserName.ToString(),
                                             DateTime.Now,
                                             null);

                object objAddID = null;

                Helper.ExecuteNonQuery(command);

                objAddID = command.Parameters["@addressid"].Value;

                return (int) objAddID;

            }

            else
            {
                string transactionid =
                    NCS.IConnect.ApplicationContexts.ApplicationContextFactory.GetApplicationContext().TransactionId;
                DbHelper helper = new DbHelper();
                DbCommand command = helper.BuildDbCommand("P_CM_Address_U");

                helper.AssignParameterValues(command,
                                             retaddressID,
                                             null,
                                             null,
                                             string.IsNullOrEmpty(row.UnitNumber) ? null : row.UnitNumber,
                                             null,
                                             null,
                                             string.IsNullOrEmpty(row.StreetNumber) ? null : row.StreetNumber,
                                             null,
                                             string.IsNullOrEmpty(row.Street) ? null : row.Street,
                                             string.IsNullOrEmpty(row.Type) ? null : row.Type,
                                             string.IsNullOrEmpty(row.Suffix) ? null : row.Suffix,
                                             null,
                                             null,
                                             string.IsNullOrEmpty(row.Suburb) ? null : row.Suburb,
                                             string.IsNullOrEmpty(row.State) ? null : row.State,
                                             string.IsNullOrEmpty(row.PostCode) ? null : row.PostCode,
                                             string.IsNullOrEmpty(address1) ? null : address1,
                                             null,
                                             null,
                                             null,
                                             null,
                                             null,
                                             0,
                                             null,
                                             null,
                                             null,
                                             null,
                                             1101,
                                             null,
                                             0,
                                             versionAddressId,
                                             transactionid,
                                             AppContext.Current.UserName.ToString(),
                                             DateTime.Now,
                                             AppContext.Current.UserName.ToString(),
                                             DateTime.Now,
                                             null);

                Helper.ExecuteNonQuery(command);
            }

            return retaddressID;
        }
        private int? IsAddressExist(LookupOrganisationalUnitDataSet.LookupOrganisationalUnitDataTable dtLookupOrganisationalUnitDataTable, string Address1,out int versionAddressId)
        {
            DataTable dtAddress = new DataTable();
            versionAddressId = 0;
            var row = dtLookupOrganisationalUnitDataTable[0];

            this.Helper.Fill(dtAddress, "P_CM_Address_S",
                              null,
                              row.Suburb,
                              row.State,
                              row.PostCode,
                              Address1,
                              null,
                              null
                              );

            if (dtAddress.Rows.Count > 0)
            {
                versionAddressId = Convert.ToInt32(dtAddress.Rows[0]["VersionNo"].ToString());
                return Convert.ToInt32(dtAddress.Rows[0]["AddressID"].ToString());
            }

            return null;
        }
        public void SaveOrganisationLookup(LookupOrganisationalUnitDataSet.LookupOrganisationalUnitDataTable dtLookupOrganisationalUnitDataTable)
        {
            string transactionid = NCS.IConnect.ApplicationContexts.ApplicationContextFactory.GetApplicationContext().TransactionId;
            DbHelper helper = new DbHelper();
            DbCommand command = helper.BuildDbCommand("P_SS_LookupOrganisationalUnit_U");

            helper.AssignParameterValues(command,
                Membership.ApplicationName,
                dtLookupOrganisationalUnitDataTable[0].OrganisationalUnitID,
                dtLookupOrganisationalUnitDataTable[0].OrganisationalUnitName,
                dtLookupOrganisationalUnitDataTable[0].VersionNo,
                transactionid,
                AppContext.Current.UserName.ToString(),
                DateTime.Now);

            Helper.ExecuteNonQuery(command);

            LookupOrganisationalUnitDataSet.LookupOrganisationalUnitDataTable dtOrgDetail = GetOrganisationalUnitDetailByOrgID(dtLookupOrganisationalUnitDataTable[0].OrganisationalUnitID);

            int? addressId = SaveAddress(dtLookupOrganisationalUnitDataTable,dtOrgDetail);

            SaveOrganisationLookupDetail(dtLookupOrganisationalUnitDataTable,dtOrgDetail,addressId);
        }
 public void SaveOrganisationLookup(LookupOrganisationalUnitDataSet.LookupOrganisationalUnitDataTable dtLookupOrganisationalUnitDataTable)
 {
     this._orgMaintenanceServiceDA.SaveOrganisationLookup(dtLookupOrganisationalUnitDataTable);
 }
        private LookupOrganisationalUnitDataSet.LookupOrganisationalUnitDataTable RetriveOrgData(LookupOrganisationalUnitDataSet.LookupOrganisationalUnitDataTable LookUpOrg, string Orgenisation)
        {
            if (LookUpOrg.Rows.Count == 0) return LookUpOrg;

            foreach (LookupOrganisationalUnitDataSet.LookupOrganisationalUnitRow row in LookUpOrg.Select(string.Empty, "OrganisationalUnitName"))
            {
                if (!this.IsMatchOrgName(Orgenisation, row.OrganisationalUnitName))
                {
                    row.Delete();
                }
            }
            LookUpOrg.AcceptChanges();

            return LookUpOrg;
        }