Click here to Skip to main content
15,909,205 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

In my asp.net web applications web form i used gridview to display list of transactions done by customer/vendors. The gridview is paging enabled, page size is 20,present there is 26 transactions are there so gridview contains 26 rows.

The problem is when ever i am trying to edit row which are present other then first page an exception is throwing by system that is:

CSS
Exception Details: System.ArgumentOutOfRangeException: Index was out of
range. Must be non-negative and less than the size of the collection.
Parameter name: index


Gridview rowdatabound code is as follows:

C#
protected void gvTransactionDetails_RowCommand(object sender, GridViewCommandEventArgs e)
 {
   if (e.CommandName == "Edit")
   {

    int igvTransactionDetailsIndex = Convert.ToInt32(e.CommandArgument);


   GridViewRow gvTransactionDetailsrow = gvTransactionDetails.Rows[igvTransactionDetailsIndex];
   Session["TDID"] = gvTransactionDetails.DataKeys[igvTransactionDetailsIndex].Value;
         
    Response.Redirect("~/AddEditTransactionDetails.aspx?AddEdit=Edit&CusVen=" +sCusVen);
           }
       }



please anybody help me to solve that error.

Thanks in advance
Uday
Posted

First, this is not nice to ask such question without showing exact line where it was thrown/propagated. You can find it out running the code under debugger or by catching all exception on top of the stack of this thread and examining Exception.Stack.

Most likely, this is the line where igvTransactionDetailsIndex is first used as an index, or in next line. If could be less than 0 or more or equal to the number of Rows or number of DataKeys. Check up the value of this variable.

—SA
 
Share this answer
 
Comments
Espen Harlinn 23-Oct-11 6:42am    
Good advice
Sergey Alexandrovich Kryukov 23-Oct-11 19:01pm    
Thank you, Espen.
--SA
udaysimha 24-Oct-11 0:50am    
Hi SAKryukov,

Thanks for your replay and advice.Here gridview contains 26 rows, size of the page i set to 20.Whenever i am trying to edit the first 20 rows which are present in first page there is no problem at all.The problem is whenever i am trying to edit rows from 21 or more it`s throwing exception.

The line igvTransactionDetailsIndex is used to find the index of row which is going to edit.it getting the index of row which is going to edit currectly but the next line that as below

GridViewRow gvTransactionDetailsrow = gvTransactionDetails.Rows[igvTransactionDetailsIndex];//exception throwing here

The gridviewrow object gvTransactionDetailsrow is returning null.

Please suggest me to solve this problem.

Thaks
Uday
please put your gridview html code so that it will clear some picture regarding command argument and datakeys parameter which will help to find out the solution as per you code i think you need to check the value of igvTransactionDetailsIndex ideally it should accept row index surely there will a solution but you need to put your gridview html code for this.
 
Share this answer
 
Comments
udaysimha 24-Oct-11 0:59am    
Hi AlokZanwar,

Thanks for your reply and suggestion.Here i am puting gridview html code:

<asp:GridView ID="gvTransactionDetails" runat="server" Style="left: -3px; position: relative;
top: 44px; width: 641px;" AutoGenerateColumns="False" ShowFooter="false" CellPadding="4"
ForeColor="#333333" GridLines="None" OnRowEditing="gvTransactionDetails_RowEditing"
OnRowCommand="gvTransactionDetails_RowCommand" OnRowDataBound="gvTransactionDetails_RowDataBound"
DataKeyNames="TDID" AllowPaging="True" onpageindexchanging="gvTransactionDetails_PageIndexChanging" PageSize="20">
<footerstyle forecolor="White" font-bold="True" backcolor="#1C5E55">
<rowstyle backcolor="#E3EAEB">
<columns>
<asp:BoundField HeaderText="CVRelID" DataField="CVRelID" Visible="false" />
<asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center">
<HeaderTemplate>
TransID</HeaderTemplate>
<itemtemplate>
<%#Eval("TransID")%>

<HeaderStyle HorizontalAlign="Center"></HeaderStyle><itemstyle horizontalalign="Center">
<asp:TemplateField Visible="false" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center">
<HeaderTemplate> VendorID</HeaderTemplate>
<itemtemplate>
<%#Eval("VendorID")%>

<HeaderStyle HorizontalAlign="Center"></HeaderStyle><itemstyle horizontalalign="Center">
<asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center">
<HeaderTemplate>
VendorName</HeaderTemplate>
<itemtemplate>
<%#Eval("VendorName")%>

<HeaderStyle HorizontalAlign="Center"></HeaderStyle><itemstyle horizontalalign="Center">
<asp:TemplateField Visible="false" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center">
<HeaderTemplate>CustomerID</HeaderTemplate>
<itemtemplate>
<%#Eval("CustomerID")%>

<HeaderStyle HorizontalAlign="Center"></HeaderStyle><itemstyle horizontalalign="Center">
<asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center">
<HeaderTemplate>CustomerName</HeaderTemplate>
<itemtemplate>
<%#Eval("CustomerName")%>

<HeaderStyle HorizontalAlign="Center"></HeaderStyle><itemstyle horizontalalign="Center">
<asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center">
<HeaderTemplate>Origin</HeaderTemplate>
<itemtemplate> <%#Eval("Origin")%>

<HeaderStyle HorizontalAlign="Center"></HeaderStyle><itemstyle horizontalalign="Center">
udaysimha 24-Oct-11 4:56am    
<asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center">

<HeaderTemplate>Destination</HeaderTemplate>

<itemtemplate> <%#Eval("Destination")%>

<HeaderStyle HorizontalAlign="Center"></HeaderStyle><itemstyle horizontalalign="Center">

<asp:TemplateField ShowHeader="False" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center"><itemtemplate><asp:ImageButton ID="ibEdit" runat="server" CausesValidation="False" CommandName="Edit"ImageUrl="~/images/Edit.gif" ToolTip="Edit" CommandArgument='<%# Container.DataItemIndex %>'><HeaderStyle HorizontalAlign="Center"></HeaderStyle><itemstyle horizontalalign="Center">

please help me to solve this error.
Thanks

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