Specified argument was out of the range of valid values.

Dear Experts I am getting attached error against the below code: Please help me what I am doing wrong.

For index = 0 To gvDetail.Rows.Count - 1
            Dim gvr As GridViewRow = gvDetail.Rows(index)
            Dim av As TextBox = gvr.Cells(index).FindControl("txtAV")
            lblMessage.Text = av.Text
        Next

Open in new window


System.ArgumentOutOfRangeException was unhandled by user code
  Message=Specified argument was out of the range of valid values.
Parameter name: index
  ParamName=index
  Source=System.Web
  StackTrace:
       at System.Web.UI.ControlCollection.get_Item(Int32 index)
       at System.Web.UI.WebControls.TableCellCollection.get_Item(Int32 index)
       at Admin_Imp_CustomAssessement.ReadGridView() in D:\Imp_App\Admin\Imp_CustomAssessment.aspx.vb:line 42
       at Admin_Imp_CustomAssessement.lnkApply_Click(Object sender, EventArgs e) in D:\Imp_App\Admin\Imp_CustomAssessment.aspx.vb:line 104
       at System.Web.UI.WebControls.LinkButton.OnClick(EventArgs e)
       at System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument)
       at System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
       at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
       at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  InnerException: 

Open in new window

MehramAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

appariCommented:
>>gvr.Cells(index).FindControl("txtAV")
you are using same variable index for both row and column index.
if you have more rows than the cells in the gridview you will get indexoutofrange error.

replace index in the above line with the cell index you want to search.
0
MehramAuthor Commented:
Can you change my code for my guidance please
0
appariCommented:
what is the cell number you are searching for the textbox?

replace the bold part with the actual cell index.

For index = 0 To gvDetail.Rows.Count - 1
            Dim gvr As GridViewRow = gvDetail.Rows(index)
            Dim av As TextBox = gvr.Cells(index).FindControl("txtAV")
            lblMessage.Text = av.Text
Next
0
MehramAuthor Commented:
changed
            Dim av As TextBox = gvr.Cells(6).FindControl("txtAV")
but same error.

My aspx code is:-

<asp:GridView ID="gvDetail" runat="server" AllowPaging="false"  AllowSorting="true" GridLines="Both"
                            CssClass="display" AutoGenerateColumns="false" DataKeyNames="Id,JobNo" Width="99%" ShowHeader="false">
                            <Columns>
                            <asp:TemplateField HeaderText="ID" Visible="true">
                                <ItemTemplate>
                                    <table width="100%">
                                        <tr>
                                            <td>
                                                <table width="100%">
                                                    <tr>
                                                        <th>ID</th>
                                                        <th>Item Code</th>
                                                        <th>Item Description</th>
                                                        <th style="text-align:right;">Quantity</th>
                                                        <th style="text-align:right;">Net Weight</th>
                                                        <th>DV</th>
                                                    </tr>
                                                    <tr>
                                                        <td><%# Eval("Id") %></td>
                                                        <td><%# Eval("ItemCode")%></td>
                                                        <td><%# Eval("ItemDiscription")%></td>
                                                        <td style="text-align:right;"><%# Eval("Qty")%></td>
                                                        <td style="text-align:right;"<%# Eval("NetWeight")%></td>
                                                        <td><%# Eval("DV")%></td>
                                                    </tr>
                                    
                                                </table>
                                            </td>
                                        </tr>
                                        
                                        <tr>
                                            <td>
                                                <table>
                                                    <tr>
                                                        <th>AV</th>
                                                        <th>AVPKR</th>
                                                        <th>CD</th>
                                                        <th>ST</th>
                                                        <th>AST</th>
                                                        <th>IT</th>
                                                        <th>FED/KG</th>
                                                        <th>FED %</th>
                                                        <th>PSQCA</th>
                                                        <th>Quarantine</th>
                                                    </tr>
                                                    <tr>
                                                        <td>
                                                            <asp:TextBox ID="txtAV" runat="server" CssClass="textbox" Text='<%# Eval("AV","{0:F2}") %>' Width="45px" style="text-align:right;"></asp:TextBox>
                                                        </td>
                                                        <td>
                                                            <asp:TextBox ID="txtAVPKR" runat="server" CssClass="textbox" Text='<%# Eval("AVPKR","{0:F2}") %>' Width="45px" style="text-align:right;"></asp:TextBox>
                                                        </td>
                                                        <td>
                                                             <asp:TextBox ID="txtCD" runat="server" CssClass="textbox"  Text='<%# Eval("CD","{0:F2}") %>' Width="45px" style="text-align:right;"></asp:TextBox>
                                                        </td>
                                                        <td>
                                                            <asp:TextBox ID="txtST" runat="server" CssClass="textbox" Text='<%# Eval("ST","{0:F2}") %>' Width="45px" style="text-align:right;"></asp:TextBox>
                                                        </td>
                                                        <td>
                                                             <asp:TextBox ID="txtAST" runat="server" CssClass="textbox" Text='<%# Eval("AST", "{0:F2}")%>' Width="45px" style="text-align:right;"></asp:TextBox>
                                                        </td>
                                                        <td>
                                                            <asp:TextBox ID="txtIT" runat="server" CssClass="textbox"  Text='<%# Eval("IT", "{0:F2}")%>' Width="45px" style="text-align:right;"></asp:TextBox>
                                                        </td>
                                                         <td>
                                                            <asp:TextBox ID="TextBox1" runat="server" CssClass="textbox"  Text='<%# Eval("FEDPerKG", "{0:F2}")%>' Width="45px" style="text-align:right;"></asp:TextBox>
                                                        </td>
                                                         <td>
                                                            <asp:TextBox ID="TextBox2" runat="server" CssClass="textbox"  Text='<%# Eval("FEDPercentage", "{0:F2}")%>' Width="45px" style="text-align:right;"></asp:TextBox>
                                                        </td>
                                                         <td>
                                                            <asp:TextBox ID="TextBox3" runat="server" CssClass="textbox"  Text='<%# Eval("PSQCA", "{0:F2}")%>' Width="45px" style="text-align:right;"></asp:TextBox>
                                                        </td>
                                                         <td>
                                                            <asp:TextBox ID="TextBox4" runat="server" CssClass="textbox"  Text='<%# Eval("Quarantine", "{0:F2}")%>' Width="45px" style="text-align:right;"></asp:TextBox>
                                                        </td>
                                                    </tr>
                                    
                                                </table>
                                            </td>
                                        </tr>
                                        <tr><td style="border-bottom:1px solid #666;"></td></tr>
                                    </table>
                                </ItemTemplate>
                            </asp:TemplateField>
                           
                           
                           

                            </Columns>
                            </asp:GridView>

Open in new window

0
appariCommented:
try with index 0 instead of 6, you are using templatefield and it is the only column in the gridview. withing the template field you are using a table.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.