Results 1 to 3 of 3

Thread: Index was out of range

  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.

  2. #2
    Hyperactive Member drattansingh's Avatar
    Join Date
    Sep 2005
    Posts
    395

    Re: Index was out of range

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


    You get this error message if you create an array like:

    int[] x = new int [4];

    then if i do x[2] = 5; // this is correct and will assign 5 to index number 1 of the array since an array starts from 0.

    But if i do x[7]=5; // I will get the above error since the size of the array is less than index number 7, it does not exist.

    The object name to be concerned in INDEX.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Index was out of range

    Please point out which line throws the exception in future, so we don't have to search through the entire code. I had a look at your code but it's not obvious where the issue is so you have to wait longer for a solution. You provided the error message, which is more than many people bother to do, but where the error occurred is always important too.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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