Results 1 to 3 of 3

Thread: Index was out of range

Threaded View

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2008
    Posts
    65

    Unhappy Index was out of range

    Halloo....

    I need your help badly

    Please do help me to solve this error...
    I got this error after i save the first record and navigate to the next record and then save.

    Thanks Thanks....

    "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index"



    Code:
    private int _TaxCodeId;
        private TaxCode _TaxCode;
        protected void Page_Load(object sender, EventArgs e)
        {
            _TaxCodeId = AlwaysConvert.ToInt(Request.QueryString["TaxCodeId"]);
            _TaxCode  = TaxCodeDataSource.Load(_TaxCodeId);
            if (_TaxCode == null) Response.Redirect("TaxCodes.aspx");
            if (!Page.IsPostBack)
            {
                Caption.Text = string.Format(Caption.Text, _TaxCode.Name);
            }
        }
    
        protected bool IsLinked(object dataItem)
        {
            TaxRule rule = (TaxRule)dataItem;
            return (rule.TaxRuleTaxCodes.IndexOf(rule.TaxRuleId, _TaxCodeId) > -1);
        }
    
        protected void Linked_CheckChanged(object sender, EventArgs e)
        {
            CheckBox linked = (CheckBox)sender;
            int taxRuleId = AlwaysConvert.ToInt(linked.Text);
            TaxRule taxRule = TaxRuleDataSource.Load(taxRuleId);
            if (linked.Checked)
            {
                //ADD IF NOT FOUND
                if (taxRule.TaxRuleTaxCodes.IndexOf(taxRuleId, _TaxCodeId) < 0)
                {
                    taxRule.TaxRuleTaxCodes.Add(new TaxRuleTaxCode(taxRuleId, _TaxCodeId));
                    taxRule.Save();
                }
            }
            else
            {
                //DELETE IF FOUND
                int index = taxRule.TaxRuleTaxCodes.IndexOf(taxRuleId, _TaxCodeId);
                if (index > -1)
                {
                    taxRule.TaxRuleTaxCodes.DeleteAt(index);
                }
            }
            TaxRuleGrid.DataBind();
        }
    
        protected void SaveButton_Click(object sender, EventArgs e)
        {
            _TaxCode.TaxRuleTaxCodes.DeleteAll();
            foreach (GridViewRow gvr in TaxRuleGrid.Rows)
            {
                CheckBox Linked = gvr.FindControl("Linked") as CheckBox;
                if (Linked.Checked)
                {
                    int taxRuleId = (int)TaxRuleGrid.DataKeys[gvr.DataItemIndex].Value;
                    _TaxCode.TaxRuleTaxCodes.Add(new TaxRuleTaxCode(taxRuleId, _TaxCodeId));
                }
            }
            _TaxCode.Save();
            SavedMessage.Visible = true;
            SavedMessage.Text = string.Format(SavedMessage.Text, LocaleHelper.LocalNow);
        }
    Last edited by hamtaro^-^; Dec 18th, 2008 at 03:11 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width