leap29
asked on
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>
ASKER
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("@descr iption", SqlDbType.VarChar).Value = GridView1.Rows(e.RowIndex) .Cells(2). Text
Line 52: cmd.Parameters.Add("@objec tid", SqlDbType.Int).Value = Convert.ToInt32(GridView1. Rows(e.Row Index).Cel ls(0).Text )
Line 53: cmd.Connection = con
Line 54: con.Open()
Source File: \\rdb1\hr\QuestionCategori es.aspx.vb Line: 52
Stack Trace:
[FormatException: Input string was not in a correct format.]
System.Number.StringToNumb er(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) +2755599
System.Number.ParseInt32(S tring s, NumberStyles style, NumberFormatInfo info) +112
System.Convert.ToInt32(Str ing value) +68
QuestionCategories.GridVie w1_RowUpda ting(Objec t sender, GridViewUpdateEventArgs e) in \\rdb1\hr\QuestionCategori es.aspx.vb :52
System.Web.UI.WebControls. GridView.O nRowUpdati ng(GridVie wUpdateEve ntArgs e) +133
System.Web.UI.WebControls. GridView.H andleUpdat e(GridView Row row, Int32 rowIndex, Boolean causesValidation) +776
System.Web.UI.WebControls. GridView.H andleEvent (EventArgs e, Boolean causesValidation, String validationGroup) +837
System.Web.UI.WebControls. GridView.O nBubbleEve nt(Object source, EventArgs e) +95
System.Web.UI.Control.Rais eBubbleEve nt(Object source, EventArgs args) +35
System.Web.UI.WebControls. GridViewRo w.OnBubble Event(Obje ct source, EventArgs e) +117
System.Web.UI.Control.Rais eBubbleEve nt(Object source, EventArgs args) +35
System.Web.UI.WebControls. LinkButton .OnCommand (CommandEv entArgs e) +115
System.Web.UI.WebControls. LinkButton .RaisePost BackEvent( String eventArgument) +132
System.Web.UI.WebControls. LinkButton .System.We b.UI.IPost BackEventH andler.Rai sePostBack Event(Stri ng eventArgument) +7
System.Web.UI.Page.RaisePo stBackEven t(IPostBac kEventHand ler sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePo stBackEven t(NameValu eCollectio n postData) +177
System.Web.UI.Page.Process RequestMai n(Boolean includeStagesBeforeAsyncPo int, Boolean includeStagesAfterAsyncPoi nt) +1746
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("@descr
Line 52: cmd.Parameters.Add("@objec
Line 53: cmd.Connection = con
Line 54: con.Open()
Source File: \\rdb1\hr\QuestionCategori
Stack Trace:
[FormatException: Input string was not in a correct format.]
System.Number.StringToNumb
System.Number.ParseInt32(S
System.Convert.ToInt32(Str
QuestionCategories.GridVie
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.Control.Rais
System.Web.UI.WebControls.
System.Web.UI.Control.Rais
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.Page.RaisePo
System.Web.UI.Page.RaisePo
System.Web.UI.Page.Process
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
cmd.Parameters.Add("@descr
with
cmd.Parameters.Add("@descr