|
-
Dec 18th, 2008, 02:46 PM
#1
Thread Starter
Lively Member
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.
-
Dec 28th, 2008, 06:22 PM
#2
Hyperactive Member
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.
-
Dec 28th, 2008, 07:27 PM
#3
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|