简体   繁体   中英

error on updating grid view values asp.net

I have usedgridView for update and delete data. But I'm getting

"Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index" error while updating.

There is no problem in your jquery datetimepicker

Problem seems to be in :

GridViewRow row = GridView1.Rows[e.RowIndex];

Simply take separate link buttons for edit and delete as:

<asp:TemplateField HeaderText="Edit" ItemStyle-Width="150">
     <ItemTemplate>
                    <asp:linkbutton id="lnkEdit" runat="server" CommandName="Edit" Text="Edit"/>
                </ItemTemplate>
</asp:TemplateField>

 <asp:TemplateField HeaderText="Delete" ItemStyle-Width="150">
     <ItemTemplate>
                    <asp:linkbutton id="lnkDelete" runat="server" CommandName="Delete" Text="Delete" />
                </ItemTemplate>
</asp:TemplateField>

I can see you are using dropdownlist for copy_id so it is better to use the below code to find the dropdownlist and fetch value from it.

 DropDownList lblCopyDdl= (DropDownList )GridView1.Rows[e.RowIndex].FindControl("lblCopyId");
 int Copy_Id = Convert.ToInt32(lblCopyDdl.selectedvalue);

Recommendation:: Better if you use the same code as mentioned above for other fields for getting value..

for eg

int Borrower_Id = Convert.ToInt32((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtBorrowerId").Text);

Update::

If it not works then do check your e.RowIndex as mentioned in Ángel Di María's answer.

Update 2

try to fetch the date field like this

 DateTime Date_Borrowed = Convert.ToDateTime((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtDateBorrowed").Text);

and

 DateTime Date_Returned= Convert.ToDateTime((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtDateReturned").Text);

It seems that you are trying to read a cell value using index but there is no data element at that position. Suggest you to do debug and increment index value in your collection in watch window and observe data

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM