Specified argument was out of the range of valid values Parameter name: index

I have a gridview that I want to be able to update certain rows of.  At the moment I get the error the below.

Specified argument was out of the range of valid values.
Parameter name: index 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
 
Exception Details: System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: index
 
Source Error: 
 
 
Line 49:         Dim cmd As New SqlCommand()
Line 50:         cmd.CommandText = "UPDATE objectdiary set shortdescription = @description where objectdiaryid = @objectid"
Line 51:         cmd.Parameters.Add("@description", SqlDbType.VarChar).Value = DirectCast(GridView1.Rows(e.RowIndex).Cells(2).Controls(0), TextBox).Text
Line 52:         cmd.Parameters.Add("@objectid", SqlDbType.Int).Value = Convert.ToInt32(GridView1.Rows(e.RowIndex).Cells(0).Text)
Line 53:         cmd.Connection = con
 
 
Source File: \\rdb1\hr\QuestionCategories.aspx.vb    Line: 51 
 
Stack Trace: 
 
 
[ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: index]
   System.Web.UI.ControlCollection.get_Item(Int32 index) +2107838
   QuestionCategories.GridView1_RowUpdating(Object sender, GridViewUpdateEventArgs e) in \\rdb1\hr\QuestionCategories.aspx.vb:51
   System.Web.UI.WebControls.GridView.OnRowUpdating(GridViewUpdateEventArgs e) +133
   System.Web.UI.WebControls.GridView.HandleUpdate(GridViewRow row, Int32 rowIndex, Boolean causesValidation) +776
   System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +837
   System.Web.UI.WebControls.GridView.OnBubbleEvent(Object source, EventArgs e) +95
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +35
   System.Web.UI.WebControls.GridViewRow.OnBubbleEvent(Object source, EventArgs e) +117
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +35
   System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +115
   System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +132
   System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +177
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1746
 
 
And this is my gridview
 
 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
            DataKeyNames="objectdiaryid" ForeColor="#333333" GridLines="None" OnRowCancelingEdit="GridView1_RowCancelingEdit"
            OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
            <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
            <Columns>
                <asp:CommandField HeaderText="Edit-Update" ShowEditButton="True" />
                
            <asp:BoundField DataField="objectdiaryid" HeaderText="objectdiaryid" 
                ReadOnly="True" SortExpression="objectdiaryid" />
            <asp:BoundField DataField="con" HeaderText="con" ReadOnly="True" 
                SortExpression="con" />
            <asp:BoundField DataField="shortdescription" HeaderText="shortdescription" 
                SortExpression="shortdescription" />
            <asp:BoundField DataField="description" HeaderText="description" 
                  ReadOnly="True" SortExpression="description" />
            <asp:BoundField DataField="Date From" HeaderText="Date From" ReadOnly="True" 
                SortExpression="Date From" />
            <asp:BoundField DataField="Date Till" HeaderText="Date Till" ReadOnly="True" 
                SortExpression="Date Till" />
            <asp:BoundField DataField="Total Number of days off" 
                HeaderText="Total Number of days off" ReadOnly="True" 
                SortExpression="Total Number of days off" />
                     <asp:CommandField HeaderText="Delete" ShowDeleteButton="True" />
            </Columns>
          
            <RowStyle BackColor="#E3EAEB" />
            <EditRowStyle BackColor="#7C6F57" />
            <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
            <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
            <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
            <AlternatingRowStyle BackColor="White" />
        </asp:GridView>

Open in new window

leap29Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

BinuthCommented:
replace

cmd.Parameters.Add("@description", SqlDbType.VarChar).Value = DirectCast(GridView1.Rows(e.RowIndex).Cells(2).Controls(0), TextBox).Text

with

cmd.Parameters.Add("@description", SqlDbType.VarChar).Value = GridView1.Rows(e.RowIndex).Cells(2).Text
0
leap29Author Commented:
Thanks for the reply.  I've amended the code but now get the below error!


Input string was not in a correct format.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.FormatException: Input string was not in a correct format.

Source Error:


Line 50:         cmd.CommandText = "UPDATE objectdiary set shortdescription = @description where objectdiaryid = @objectid"
Line 51:         cmd.Parameters.Add("@description", SqlDbType.VarChar).Value = GridView1.Rows(e.RowIndex).Cells(2).Text
Line 52:         cmd.Parameters.Add("@objectid", SqlDbType.Int).Value = Convert.ToInt32(GridView1.Rows(e.RowIndex).Cells(0).Text)
Line 53:         cmd.Connection = con
Line 54:         con.Open()
 

Source File: \\rdb1\hr\QuestionCategories.aspx.vb    Line: 52

Stack Trace:


[FormatException: Input string was not in a correct format.]
   System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) +2755599
   System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) +112
   System.Convert.ToInt32(String value) +68
   QuestionCategories.GridView1_RowUpdating(Object sender, GridViewUpdateEventArgs e) in \\rdb1\hr\QuestionCategories.aspx.vb:52
   System.Web.UI.WebControls.GridView.OnRowUpdating(GridViewUpdateEventArgs e) +133
   System.Web.UI.WebControls.GridView.HandleUpdate(GridViewRow row, Int32 rowIndex, Boolean causesValidation) +776
   System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +837
   System.Web.UI.WebControls.GridView.OnBubbleEvent(Object source, EventArgs e) +95
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +35
   System.Web.UI.WebControls.GridViewRow.OnBubbleEvent(Object source, EventArgs e) +117
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +35
   System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +115
   System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +132
   System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +177
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1746

 
0
BinuthCommented:
Convert.ToInt32(GridView1.Rows(e.RowIndex).Cells(0).Text)

make sure that e.RowIndex).Cells(0).Text  not contains null/empty string...
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
.NET Programming

From novice to tech pro — start learning today.