[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5216
  • Last Modified:

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

0
leap29
Asked:
leap29
  • 2
1 Solution
 
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

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now