Click here to Skip to main content
15,176,980 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using ajax maksed edit extender for calender control.
but using this when i click the edit button of my grid it does not react and using debug it shows the error that Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index on edit...
when i commented the ajax masked edit extender the grid works perfect with edit.
So tell me what is the problem?????
Thanx in advance...
_Amy 31-Aug-12 2:48am
Where is the code?
bbirajdar 31-Aug-12 2:50am
There is problem in your code. You need to check the Items count before you use the collection item with index
Adittya Gupta 5-Sep-12 5:22am
Can you post your code or error stack trace this will help us in understanding problem more clearly.

The error means you are accessing a collection with index less than zero or greater than its capacity. Note that the index is 0 based so if you have 3 items, the available indexes are 0,1,2.
Try stepping through your code and you will be able to figure out which collection is throwing this error.

this error comes when there is no such index available in object

it's about you have 3 cards B,C,D but I am asking for 'A' or 'Z'

grid1 have 2 rows
rowno   index
1       0
2       1

Now if we try to fetch 3rd row (means index=2) this kind of error occur
grid1.rows[2].cells[0].value // it will throw exception

check your code some where same thing happened as above example

to solve error,
write protective conditions so that such situation never occur
if (RowId > grid1.rows.count-1) //condition is protecting inner statement so, if suppose RowId=3 then it will not go to execute inner statement

Happy Coding!

As you have not post your code along with question we can just assume how your code would be.

IndexOutOfRange exception occur when you are requesting for nonexisting index in the Array or Collection.

How to reproduce this exception : Example
int[] array = new int[100];
array[120] = 3; // Here 120 index is not exist as array is of size 100

For more information Refer : MSDN IndexOutOfRangeException[^]

Hope you findout your issue yourself. Next time when you post your question, also post the line of code where you are getting exception.

-Amit Gajjar
sasha rahil 31-Aug-12 3:25am
sorry for not adding code
here is my code

public void SetEditRowData(int val)
if (dgProductCategory.Rows.Count == 0) return;
dgProductCategory.Rows[val].BackColor = Color.FromName(cCommonValues.gridhighlighted);
List<ProductCategoryEntity> _listView = (List<ProductCategoryEntity>)ViewState["list"];

foreach (ProductCategoryEntity item in _listView)
if (item.product_category_id == Convert.ToInt32(ViewState["product_category_id"]))
dtpactivefrom.Text = Convert.ToString(item.active_from);
dtpactiveto.Text = Convert.ToString(item.active_to);
txtengname.Value = item.engname.ToString();
txtothername.Value = item.othername.ToString();

if (Session["EditMode"] != null)
btnSave.Visible = false;
btnSave.Text = Resources.Resource.Update_Button.ToString();
btnSave.Visible = true;


catch (Exception ex)
AmitGajjar 31-Aug-12 3:50am
And on which line you are getting this error?
I just used Onclientclick for the edit button and it worked.....
Strangest but worked....
The other option which i tried and worked was commenting the maskededitvalidator and using regular expression in stead of that...and it worked too..
Index out of range, the code posted a look!Or send to my mailbox!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900