Dropdownlist控件值未保存到数据库

时间:2014-08-13 13:11:23

标签: c# sql drop-down-menu

我在添加/编辑表单页面上有两个下拉列表控件,一个用于国家/地区(称为CountryCode),另一个用于区域/状态(称为ProvinceCode)。已设置代码,以便当用户从国家/地区下拉列表中选择“美国”时,区域下拉列表将填充所有有效的美国州。如果用户从国家/地区下拉列表中选择“加拿大”,则区域下拉列表中将填入有效省份。此功能正常,但是当选择“加拿大”时,由于某种原因,区域下拉列表的值永远不会保存到数据库中。我不确定为什么会这样,特别是因为我可以拯救美国各州。

这是用于填充国家和地区下拉列表的代码:

    protected void Page_Init(object sender, EventArgs e)
    {
        CountryCode.DataSource = CountryDataSource.LoadAll("Name");
        CountryCode.DataBind();

        InitCountryAndProvince();

    }

    private void InitCountryAndProvince()
    {
        //MAKE SURE THE CORRECT ADDRESS IS SELECTED
        Address address = this.Address;
        bool foundCountry = false;
        if (!string.IsNullOrEmpty(address.CountryCode))
        {
            ListItem selectedCountry = CountryCode.Items.FindByValue(address.CountryCode);
            if (selectedCountry != null)
            {
                CountryCode.SelectedIndex = CountryCode.Items.IndexOf(selectedCountry);
                foundCountry = true;
            }
        }
        if (!foundCountry)
        {
            Warehouse defaultWarehouse = AbleContext.Current.Store.DefaultWarehouse;
            ListItem selectedCountry = CountryCode.Items.FindByValue(defaultWarehouse.CountryCode);
            if (selectedCountry != null) CountryCode.SelectedIndex = CountryCode.Items.IndexOf(selectedCountry);
        }
        //MAKE SURE THE PROVINCE LIST IS CORRECT FOR THE COUNTRY
        UpdateCountry();
        //NOW LOOK FOR THE PROVINCE TO SET
        ListItem selectedProvince = ProvinceCode.Items.FindByValue(address.Province);
        if (selectedProvince != null) ProvinceCode.SelectedIndex = ProvinceCode.Items.IndexOf(selectedProvince);

    }

    private void UpdateCountry()
    {
        //SEE WHETHER POSTAL CODE IS REQUIRED
        string[] countries = AbleContext.Current.Store.Settings.PostalCodeCountries.Split(",".ToCharArray());
        //PostalCodeRequired.Enabled = (Array.IndexOf(countries, Country.SelectedValue) > -1);
        //SEE WHETHER PROVINCE LIST IS DEFINED
        IList<Province> provinces = ProvinceDataSource.LoadForCountry(CountryCode.SelectedValue);
        if (provinces.Count > 0)
        {
            IEnumerable<Province> sortedProvinces = provinces.OrderBy(f => f.Name);
            provinces = sortedProvinces.ToList();
            ProvinceCode.Visible = false;
            ProvinceCode.Visible = true;
            ProvinceCode.Items.Clear();
            ProvinceCode.Items.Add(string.Empty);
            foreach (Province province in provinces)
            {
                string provinceValue = (!string.IsNullOrEmpty(province.ProvinceCode) ? province.ProvinceCode : province.Name);
                ProvinceCode.Items.Add(new ListItem(province.Name, provinceValue));
            }
            ListItem selectedProvince = FindSelectedProvince();
            if (selectedProvince != null) selectedProvince.Selected = true;
            ProvinceCode.Enabled = true;
            ProvinceCode.Text = string.Empty;
        }
        else
        {
            ProvinceCode.Visible = true;
            ProvinceCode.Visible = false;
            ProvinceCode.Items.Clear();
            //Province2Required.Enabled = false;
        }
    }

    private ListItem FindSelectedProvince()
    {
        string defaultValue = ProvinceCode.Text;
        if (string.IsNullOrEmpty(defaultValue)) defaultValue = Request.Form[ProvinceCode.UniqueID];
        if (string.IsNullOrEmpty(defaultValue)) return null;
        defaultValue = defaultValue.ToUpperInvariant();
        foreach (ListItem item in ProvinceCode.Items)
        {
            string itemText = item.Text.ToUpperInvariant();
            string itemValue = item.Value.ToUpperInvariant();
            if (itemText == defaultValue || itemValue == defaultValue) return item;
        }
        return null;
    }

    protected void Country_Changed(object sender, EventArgs e)
    {
        //UPDATE THE FORM FOR THE NEW COUNTRY
        UpdateCountry();
    }

这是保存方法的一部分,其中包含国家和地区下拉列表控件:

    private void SaveCustomerInfo(int CustID)
    {
        int currentUserID = AbleContext.Current.UserId;
        string editQuery = "UPDATE Customers SET BillToRegion = @BillToRegion, BillToCountry = @BillToCountry WHERE CustomerID = @CustomerID";
        string addQuery = "INSERT INTO Customers (BillToRegion, BillToCountry) VALUES(@BillToRegion, @BillToCountry)";
        try
        {
            using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["AbleCommerce"].ToString()))
            {
                cn.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = cn;

                    cmd.Parameters.Add(new SqlParameter("@BillToRegion", ProvinceCode.SelectedValue));
                    cmd.Parameters.Add(new SqlParameter("@BillToCountry",  CountryCode.SelectedValue));

                    cmd.ExecuteNonQuery();
                    cn.Close();
                }
            }
            catch (Exception exception)
            {
                Logger.Warn("Admin\\People\\Customers\\EditCustomer.aspx - SaveCustomerInfo", exception);

            }

这是在页面加载以使用数据库中的值填充字段时调用的代码(如果用户正在编辑现有条目):

protected void Page_Load(object sender, EventArgs e)
    {
        _CustomerID = AlwaysConvert.ToInt(Request.QueryString["CustomerID"]);
        int.TryParse(Request.QueryString["CustomerID"], out _CustomerID);

        if (_CustomerID == 0)
        {

            AddBtn.Visible = true;
            EditBtn.Visible = false;
        }
        else
        {
            custIDHidden.Value = _CustomerID.ToString();
            AddBtn.Visible = false;
            EditBtn.Visible = true;

        }
        if (!Page.IsPostBack)
        {
            if (_CustomerID != 0)
            {

                string selectQuery = "BillToRegion, BillToCountry FROM Customers WHERE CustomerID = @CustomerID";
                try
                {
                    using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["AbleCommerce"].ToString()))
                    {
                        cn.Open();
                        SqlCommand cmd = new SqlCommand(selectQuery, cn);
                        cmd.Parameters.Add(new SqlParameter("@CustomerID", custIDHidden.Value));

                        using (IDataReader reader = cmd.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                ProvinceCode.SelectedValue = reader["BillToRegion"].ToString();
                                CountryCode.SelectedValue = reader["BillToCountry"].ToString();


                            }
                        }
                        cn.Close();
                    }
                }
                catch (Exception x)
                {
                    Logger.Warn("Admin\\People\\Customers\\EditCustomer.aspx - Page_Load", x);
                }

            }
        }
    }

0 个答案:

没有答案