ayha1999
asked on
Paging in GridView
When click on the second page of a gridview I get the following error;
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
error line;
Line 213: string Assignid = ((Label)(GridView1.Rows[ro wIndex].Fi ndControl( "lblAssign Id"))).Tex t.ToString ();
the error is generated from this line of markup
<asp:TemplateField ShowHeader="False" meta:resourcekey="Template FieldResou rce5">
<ItemTemplate>
<asp:LinkButton ID="lbA" runat="server" CausesValidation="False" CommandName="Ap" CommandArgument='<%# Container.DisplayIndex %>' Text="Approve" meta:resourcekey="lbApprov eResource1 "></asp:Li nkButton>
</ItemTemplate>
</asp:TemplateField>
code-behind
protected void ItemCommand(object sender, GridViewCommandEventArgs e)
{
int rowIndex = Convert.ToInt32(e.CommandA rgument);
string Assignid = ((Label)(GridView1.Rows[ro wIndex].Fi ndControl( "lblAssign Id"))).Tex t.ToString ();
pls help me tofix the error.
thanks
ayha
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
error line;
Line 213: string Assignid = ((Label)(GridView1.Rows[ro
the error is generated from this line of markup
<asp:TemplateField ShowHeader="False" meta:resourcekey="Template
<ItemTemplate>
<asp:LinkButton ID="lbA" runat="server" CausesValidation="False" CommandName="Ap" CommandArgument='<%# Container.DisplayIndex %>' Text="Approve" meta:resourcekey="lbApprov
</ItemTemplate>
</asp:TemplateField>
code-behind
protected void ItemCommand(object sender, GridViewCommandEventArgs e)
{
int rowIndex = Convert.ToInt32(e.CommandA
string Assignid = ((Label)(GridView1.Rows[ro
pls help me tofix the error.
thanks
ayha
ASKER
rowIndex value is value of "Container.DisplayIndex": from the following field
<asp:LinkButton ID="lbA" runat="server" CausesValidation="False" CommandName="Ap" CommandArgument='<%# Container.DisplayIndex %>' Text="Approve" meta:resourcekey="lbApprov eResource1 "></asp:Li nkButton>
<asp:LinkButton ID="lbA" runat="server" CausesValidation="False" CommandName="Ap" CommandArgument='<%# Container.DisplayIndex %>' Text="Approve" meta:resourcekey="lbApprov
But what is the value 2, -37, 987657....
ASKER
did I mentioned 2, -37, 987657.... anywhere in my question? only two rows there in my gridview. when click on the 2nd page the error occurs.
From your question
Index was out of range. Must be non-negative and less than the size of the collection.
I am asking you, now for the third time, what is the value of the index. Believe me it should help in solving your problem.
Index was out of range. Must be non-negative and less than the size of the collection.
I am asking you, now for the third time, what is the value of the index. Believe me it should help in solving your problem.
ASKER
value of rowIndex is 2.
OK, how many rows do you have in your gridview?
ASKER
only two rows.
That is your problem.
The first row has an index of 0, the second has an index of 1.
The first row has an index of 0, the second has an index of 1.
ASKER
yes. how can I fix it?
The obvious fix is
int rowIndex = Convert.ToInt32(e.CommandA rgument) - 1;
but I don't know if you actually send a zero for the first row. It all depends on what you use for the e.CommandArgument
int rowIndex = Convert.ToInt32(e.CommandA
but I don't know if you actually send a zero for the first row. It all depends on what you use for the e.CommandArgument
ASKER
the purpose of the rowIndex is:
protected void ItemCommand(object sender, GridViewCommandEventArgs e)
{
int rowIndex = Convert.ToInt32(e.CommandA rgument);
string RequestId = ((Label)(GridView1.Rows[ro wIndex].Fi ndControl( "lblReques tId"))).Te xt;
if (User.IsInRole("Admin"))
{
Response.Redirect("~/PassE ntry.aspx? RequestId= " + RequestId.ToString());
}
else
protected void ItemCommand(object sender, GridViewCommandEventArgs e)
{
int rowIndex = Convert.ToInt32(e.CommandA
string RequestId = ((Label)(GridView1.Rows[ro
if (User.IsInRole("Admin"))
{
Response.Redirect("~/PassE
}
else
I don't see your complete code, I don't know what it should be doing.
I have helped you find the error and told you why you get the error. Exactly what you do is really up to your requirements.
IF the index is always 1 too many then just subtract 1.
If the index is usually correct then you need to check if the index is greater than the number of rows (or less than zero) and take appropriate action (leave the function / give a warning / ....)
I have helped you find the error and told you why you get the error. Exactly what you do is really up to your requirements.
IF the index is always 1 too many then just subtract 1.
If the index is usually correct then you need to check if the index is greater than the number of rows (or less than zero) and take appropriate action (leave the function / give a warning / ....)
ASKER
even , int rowIndex = Convert.ToInt32(e.CommandA rgument) - 1; give me the same error.
What is the value of rowIndex when that gives the error ?
ASKER
rowIndex=1
OK.
This is what you say
GridView1.Rows[1] gives you an 'Index out of range' error, and GridView1 has 2 rows at that point.
That makes me think that GridView1 isn't what you think it is.
Have some code like this AND CHECK THE VALUES
int rowIndex = Convert.ToInt32(e.CommandA rgument) - 1;
int rowCount = GridView1.Rows.Count;
This is what you say
GridView1.Rows[1] gives you an 'Index out of range' error, and GridView1 has 2 rows at that point.
That makes me think that GridView1 isn't what you think it is.
Have some code like this AND CHECK THE VALUES
int rowIndex = Convert.ToInt32(e.CommandA
int rowCount = GridView1.Rows.Count;
ASKER
rowIndex = 1
rowCount = 1
rowCount = 1
rowCount = 1 means (as I told you earlier) that the only valid index is 0. Hence the error.
ASKER
so what is the solution here? I think by changing the following lines it can be solved.
int rowIndex = Convert.ToInt32(e.CommandA rgument);
string RequestId = ((Label)(GridView1.Rows[ro wIndex].Fi ndControl( "lblReques tId"))).Te xt;
I want to retrive the value of lblRequestId Label in the item command. that's what I am doing here. is there any other way I can retrive the value?
but I have no idea how to do it.
int rowIndex = Convert.ToInt32(e.CommandA
string RequestId = ((Label)(GridView1.Rows[ro
I want to retrive the value of lblRequestId Label in the item command. that's what I am doing here. is there any other way I can retrive the value?
but I have no idea how to do it.
I don't know where this e.CommandArgument comes from.
I also don't know if this function is being called multiple times, sometimes with the value you expect and then with an out of range value.
You should know these things AND, more importantly, what you need to do in those cases.
I **guess** you have always a value 1 more than the actual row index.
I also don't know if this function is being called multiple times, sometimes with the value you expect and then with an out of range value.
You should know these things AND, more importantly, what you need to do in those cases.
I **guess** you have always a value 1 more than the actual row index.
ASKER
e.CommandArugment comes from;
<asp:LinkButton ID="lbA" runat="server" CausesValidation="False" CommandName="Ap" CommandArgument='<%# Container.DisplayIndex %>' Text="Approve" meta:resourcekey="lbApprov eResource1 "></asp:Li nkButton>
I have linkbuttons in the gridvew1. when user click on any link button, I want to retrieve the value of the clicked row. (value of any cell(0) for example.) to find the clicked row I use the above e.CommandArugment.
is there any other way to get the value? I think that will solve the paging problem.
thanks
<asp:LinkButton ID="lbA" runat="server" CausesValidation="False" CommandName="Ap" CommandArgument='<%# Container.DisplayIndex %>' Text="Approve" meta:resourcekey="lbApprov
I have linkbuttons in the gridvew1. when user click on any link button, I want to retrieve the value of the clicked row. (value of any cell(0) for example.) to find the clicked row I use the above e.CommandArugment.
is there any other way to get the value? I think that will solve the paging problem.
thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
without deducting it the problem is solved.
if (e.CommandName == "CreateWo")
{
int rowIndex = Convert.ToInt32(e.CommandA rgument);
string RequestId = ((Label)(GridView1.Rows[ro wIndex].Fi ndControl( "lblReques tId"))).Te xt;
}
else if (e.CommandName == "Approve") <----- only 'else' was here. I changed to 'else if'
{
int rowIndex = Convert.ToInt32(e.CommandA rgument);
// int rowCount = GridView1.Rows.Count;
thank u very much for your time and support
if (e.CommandName == "CreateWo")
{
int rowIndex = Convert.ToInt32(e.CommandA
string RequestId = ((Label)(GridView1.Rows[ro
}
else if (e.CommandName == "Approve") <----- only 'else' was here. I changed to 'else if'
{
int rowIndex = Convert.ToInt32(e.CommandA
// int rowCount = GridView1.Rows.Count;
thank u very much for your time and support
And what is the value of rowIndex ?