/// <summary>
        /// Binds the form.
        /// </summary>
        private void BindForm()
        {
            // bind available Shipping gateway classes
            BindClassNames();

            if (_ShippingMethodDto != null && _ShippingMethodDto.ShippingOption.Count > 0)
            {
                try
                {
                    ShippingMethodDto.ShippingOptionRow shippingRow = _ShippingMethodDto.ShippingOption[0];

                    this.lblShippingMethodId.Text = shippingRow.ShippingOptionId.ToString();
                    this.tbName.Text        = shippingRow.Name;
                    this.tbDescription.Text = shippingRow.Description;
                    this.tbSystemName.Text  = shippingRow.SystemKeyword;

                    ManagementHelper.SelectListItem(ddlClassName, shippingRow.ClassName);

                    // do not allow to change system name
                    this.tbSystemName.Enabled = false;
                }
                catch (Exception ex)
                {
                    DisplayErrorMessage("Error during binding form: " + ex.Message);
                }
            }
            else
            {
                // set default form values
                this.tbSystemName.Enabled = true;
            }
        }
        /// <summary>
        /// Binds the shipping config.
        /// </summary>
        private void BindShippingConfig()
        {
            if (this.phAdditionalParameters.Controls.Count > 0)
            {
                return;
            }

            if (_ShippingMethodDto != null && _ShippingMethodDto.ShippingOption.Count > 0)
            {
                try
                {
                    ShippingMethodDto.ShippingOptionRow shippingRow = _ShippingMethodDto.ShippingOption[0];
                    // Load dynamic configuration form
                    System.Web.UI.Control ctrl = null;
                    String mainPath            = string.Concat(_ShippingGatewayConfigurationBasePath, shippingRow.SystemKeyword, _ShippingGatewayConfigurationFileName);
                    if (System.IO.File.Exists(Server.MapPath(mainPath)))
                    {
                        ctrl = base.LoadControl(mainPath);
                    }
                    else
                    {
                        ctrl = base.LoadControl(string.Concat(_ShippingGatewayConfigurationBasePath, "Generic", _ShippingGatewayConfigurationFileName));
                    }

                    if (ctrl != null)
                    {
                        ctrl.ID = shippingRow.SystemKeyword;
                        IGatewayControl tmpCtrl = (IGatewayControl)ctrl;
                        tmpCtrl.LoadObject(_ShippingMethodDto);
                        tmpCtrl.ValidationGroup = "vg" + shippingRow.SystemKeyword;

                        this.phAdditionalParameters.Controls.Add(ctrl);

                        ctrl.DataBind();
                    }
                }
                catch (Exception ex)
                {
                    DisplayErrorMessage("Error during binding additional gateway parameters: " + ex.Message);
                    return;
                }
            }
        }
        /// <summary>
        /// Binds the form.
        /// </summary>
        private void BindForm()
        {
            if (_ShippingMethodDto != null && _ShippingMethodDto.ShippingOption.Count > 0)
            {
                try
                {
                    ShippingMethodDto.ShippingOptionRow shippingRow = _ShippingMethodDto.ShippingOption[0];

                    lvMain.DataSource = _ShippingMethodDto.ShippingPackage.DefaultView;
                    lvMain.DataBind();
                }
                catch (Exception ex)
                {
                    DisplayErrorMessage("Error during binding form: " + ex.Message);
                }
            }
            else
            {
                // set default form values
            }
        }
        /// <summary>
        /// Saves the changes.
        /// </summary>
        /// <param name="context">The context.</param>
        public void SaveChanges(IDictionary context)
        {
            ShippingMethodDto dto = (ShippingMethodDto)context[_ShippingMethodDtoString];

            ShippingMethodDto.ShippingOptionRow row = null;

            if (dto == null)
            {
                // dto must be created in base Shipping control that holds tabs
                return;
            }

            // create the row if it doesn't exist; or update its modified date if it exists
            if (dto.ShippingOption.Count > 0)
            {
                row = dto.ShippingOption[0];
            }
            else
            {
                row = dto.ShippingOption.NewShippingOptionRow();
                row.ApplicationId    = OrderConfiguration.Instance.ApplicationId;
                row.ShippingOptionId = Guid.NewGuid();
                row.Created          = DateTime.UtcNow;
                row.SystemKeyword    = this.tbSystemName.Text;
            }

            // fill the row with values
            row.Modified    = DateTime.UtcNow;
            row.Name        = tbName.Text;
            row.ClassName   = ddlClassName.SelectedValue;
            row.Description = tbDescription.Text;

            // add the row to the dto
            if (row.RowState == DataRowState.Detached)
            {
                dto.ShippingOption.Rows.Add(row);
            }
        }
        private ShippingMethodDto.ShippingMethodRow CreateShippingMethod(ShippingMethodDto dto, ShippingMethodDto.ShippingOptionRow shippingOption, string languageId, int sortOrder, string name, string description, Money costInUsd, Currency currency)
        {
            Money shippingCost = CurrencyFormatter.ConvertCurrency(costInUsd, currency);

            if (shippingCost.Currency != currency)
            {
                throw new InvalidOperationException("Cannot convert to currency " + currency + " Missing conversion data.");
            }
            return(dto.ShippingMethod.AddShippingMethodRow(
                       Guid.NewGuid(),
                       shippingOption,
                       languageId,
                       true,
                       name,
                       "",
                       shippingCost.Amount,
                       shippingCost.Currency,
                       description,
                       false,
                       sortOrder,
                       DateTime.Now,
                       DateTime.Now));
        }
        /// <summary>
        /// Saves the changes.
        /// </summary>
        /// <param name="context">The context.</param>
        public void SaveChanges(IDictionary context)
        {
            ShippingMethodDto dto = (ShippingMethodDto)context[_ShippingMethodDtoString];

            ShippingMethodDto.ShippingMethodRow row = null;

            if (dto == null)
            {
                // dto must be created in base Shipping control that holds tabs
                return;
            }

            // create the row if it doesn't exist; or update its modified date if it exists
            if (dto.ShippingMethod.Count > 0)
            {
                row = dto.ShippingMethod[0];
            }
            else
            {
                row = dto.ShippingMethod.NewShippingMethodRow();
                row.ApplicationId    = OrderConfiguration.Instance.ApplicationId;
                row.ShippingMethodId = Guid.NewGuid();
                row.Created          = DateTime.UtcNow;
                row.Name             = this.tbName.Text;
            }

            // fill the row with values
            row.Modified    = DateTime.UtcNow;
            row.DisplayName = tbFriendlyName.Text;
            row.Description = tbDescription.Text;
            Guid shippingOption = Guid.Empty;

            try
            {
                shippingOption = new Guid(ddlShippingOption.SelectedValue);
            }
            catch { }

            //add ShippingOption row in dataset for ForeignKeyConstraint requirement.
            if (row["ShippingOptionId"] != DBNull.Value && !shippingOption.Equals(row.ShippingOptionId))
            {
                string shippingOptionFilter = String.Format("ShippingOptionId = '{0}'", shippingOption);
                if (dto.ShippingOption.Select(shippingOptionFilter).Length == 0)
                {
                    string            language = ddlLanguage.SelectedValue;
                    ShippingMethodDto sm       = ShippingManager.GetShippingMethods(language, true);
                    ShippingMethodDto.ShippingOptionRow[] shippingOptionRows = (ShippingMethodDto.ShippingOptionRow[])sm.ShippingOption.Select(shippingOptionFilter);
                    if (shippingOptionRows != null && shippingOptionRows.Length > 0)
                    {
                        ShippingMethodDto.ShippingOptionRow newShippingOptionRow = dto.ShippingOption.NewShippingOptionRow();
                        newShippingOptionRow.ItemArray = shippingOptionRows[0].ItemArray;
                        dto.ShippingOption.AddShippingOptionRow(newShippingOptionRow);
                        dto.ShippingOption[dto.ShippingOption.Count - 1].AcceptChanges();
                    }
                }
            }

            row.ShippingOptionId = shippingOption;
            row.BasePrice        = decimal.Parse(tbBasePrice.Text);
            row.Currency         = ddlCurrency.SelectedValue;
            row.LanguageId       = ddlLanguage.SelectedValue;
            row.IsActive         = IsActive.IsSelected;
            row.IsDefault        = IsDefault.IsSelected;
            row.Ordering         = Int32.Parse(tbSortOrder.Text);

            // add the row to the dto
            if (row.RowState == DataRowState.Detached)
            {
                dto.ShippingMethod.Rows.Add(row);
            }
        }