protected void btnSubmit_Click(object sender, EventArgs e)
        {
            resetError("", false);
            StringBuilder sql = new StringBuilder(2500);

            string  zip       = txtZip.Text.Trim();
            decimal tax       = Localization.ParseNativeDecimal(txtTax.Text.Trim());
            string  countryId = ddlCountry.SelectedValue.Trim();

            if (!CommonLogic.IsStringNullOrEmpty(countryId))
            {
                if (AppLogic.ValidatePostalCode(zip, Convert.ToInt32(countryId)))
                {
                    // ok to add them:
                    ZipTaxRate ztr = AppLogic.ZipTaxRatesTable[CommonLogic.Left(zip, 10), AppLogic.AppConfigNativeInt("Admin_DefaultTaxClassID"), Convert.ToInt32(countryId)];
                    if (ztr == null)
                    {
                        AppLogic.ZipTaxRatesTable.Add(CommonLogic.Left(zip, 10), AppLogic.AppConfigNativeInt("Admin_DefaultTaxClassID"), tax, Convert.ToInt32(countryId));
                        resetError("Zip Code added.", false);
                    }
                    else
                    {
                        ztr.Update(tax, Convert.ToInt32(countryId));
                        resetError("Zip Code already exists and was updated.", false);
                    }
                }
                else
                {
                    resetError(AppLogic.GetCountryPostalErrorMessage(Convert.ToInt32(countryId), cust.SkinID, cust.LocaleSetting), true);
                }
            }
            buildGridData();

            ShowAddPanel(false);
        }
        protected void btnUpdateOrder_Click(object sender, EventArgs e)
        {
            for (int i = 0; i <= Request.Form.Count - 1; i++)
            {
                //TR_CLASSID_ZipCode
                if (Request.Form.Keys[i].IndexOf("TR_") != -1)
                {
                    String[]    keys         = Request.Form.Keys[i].Split('_');
                    string      ZipCode      = keys[2];
                    int         ClassID      = Localization.ParseUSInt(keys[1]);
                    decimal     taxrate      = Decimal.Zero;
                    GridViewRow row          = gMain.Rows[Convert.ToInt32(keys[3])];
                    HiddenField hdfCountryID = (HiddenField)row.FindControl("hdfCountryID");
                    int         CountryID    = Convert.ToInt32(hdfCountryID.Value);

                    resetError("Items updated", false);

                    try
                    {
                        taxrate = Localization.ParseNativeDecimal(Request.Form[Request.Form.Keys[i]]);
                    }
                    catch { }
                    ZipTaxRate ztr = AppLogic.ZipTaxRatesTable[ZipCode, ClassID, CountryID];
                    try
                    {
                        if (ztr == null)
                        {
                            AppLogic.ZipTaxRatesTable.Add(ZipCode, ClassID, taxrate, CountryID);
                        }
                        else
                        {
                            ztr.Update(taxrate, CountryID);
                        }
                    }
                    catch (Exception ex)
                    {
                        string err = ex.Message;
                    }
                }
            }


            buildGridData();
        }
        protected void gMain_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            GridViewRow row = gMain.Rows[e.RowIndex];

            if (row != null)
            {
                string orig              = ViewState["OriginalZip"].ToString();
                string zip               = (((TextBox)row.FindControl("txtZip")).Text.Trim()).ToString();
                string countryid         = ((DropDownList)row.FindControl("ddlCountry")).SelectedValue.Trim();
                string OriginalCountryID = AppLogic.GetCountryID(ViewState["OriginalCountryID"].ToString()).ToString();

                StringBuilder sql = new StringBuilder(1024);

                //make sure no duplicates
                if (!orig.Equals(zip))
                {
                    int count = DB.GetSqlN(String.Format("SELECT count(*) AS N FROM ZipTaxRate WHERE ZipCode = {0} and CountryId = {1}", DB.SQuote(zip), CommonLogic.IIF(someCountryRequirePostalCode > 0, Convert.ToInt32(countryid), -1)));
                    if (count > 0)
                    {
                        resetError("Duplicate Zip Code exists", true);
                        return;
                    }
                }

                try
                {
                    resetError("Item updated", false);
                    gMain.EditIndex        = -1;
                    ViewState["SQLString"] = selectSQL;

                    for (int i = 0; i <= Request.Form.Count - 1; i++)
                    {
                        //TR_CLASSID_ZipCode
                        if (Request.Form.Keys[i].IndexOf("TR_") != -1)
                        {
                            String[] keys    = Request.Form.Keys[i].Split('_');
                            string   ZipCode = keys[2];
                            int      ClassID = Localization.ParseUSInt(keys[1]);
                            decimal  taxrate = Decimal.Zero;
                            if (ZipCode == orig && e.RowIndex == Convert.ToInt32(keys[3]))
                            {
                                try
                                {
                                    taxrate = Localization.ParseNativeDecimal(Request.Form[Request.Form.Keys[i]]);
                                }
                                catch { }

                                ZipTaxRate ztr3 = AppLogic.ZipTaxRatesTable[zip, ClassID, Convert.ToInt32(countryid)];
                                try
                                {
                                    if (ztr3 != null)
                                    {
                                        if (Convert.ToInt32(countryid) != Convert.ToInt32(OriginalCountryID))
                                        {
                                            resetError("Zip Code, Tax Class ID and Country ID already exists.", false);
                                        }
                                        else
                                        {
                                            ztr3.Update(taxrate, Convert.ToInt32(countryid));
                                        }
                                    }
                                    else
                                    {
                                        bool validZipFormat = true;
                                        if ((Convert.ToInt32(countryid) != Convert.ToInt32(OriginalCountryID)) || (orig != zip))
                                        {
                                            if (Convert.ToInt32(countryid) != Convert.ToInt32(OriginalCountryID))
                                            {
                                                validZipFormat = AppLogic.ValidatePostalCode(zip, Convert.ToInt32(countryid));
                                            }
                                            if (validZipFormat)
                                            {
                                                ZipTaxRates ztr2 = new ZipTaxRates();
                                                foreach (ZipTaxRate ztr in ztr2.All)
                                                {
                                                    if (ztr.ZipCode == orig && ztr.TaxClassID == ClassID && ztr.CountryID == Convert.ToInt32(OriginalCountryID))
                                                    {
                                                        ztr.Update(taxrate, zip, Convert.ToInt32(countryid), Convert.ToInt32(OriginalCountryID));
                                                        AppLogic.ZipTaxRatesTable.AddNewRate(ztr.ZipTaxID, zip, ClassID, taxrate, Convert.ToInt32(countryid));
                                                    }
                                                }
                                            }
                                            else
                                            {
                                                resetError(AppLogic.GetCountryPostalErrorMessage(Convert.ToInt32(countryid), cust.SkinID, cust.LocaleSetting), true);
                                            }
                                        }
                                        else
                                        {
                                            AppLogic.ZipTaxRatesTable.Add(ZipCode, ClassID, taxrate, Convert.ToInt32(countryid));
                                        }
                                    }
                                }
                                catch (Exception ex)
                                {
                                    string err = ex.Message;
                                }
                            }
                        }
                    }
                    buildGridData();
                }
                catch (Exception ex)
                {
                    throw new Exception("Couldn't update database: " + sql.ToString() + ex.ToString());
                }
            }
        }