0

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.

11
  • post full aspx of grid...i am not able to see your edit button in grid Commented Nov 12, 2014 at 6:46
  • @ÁngelDiMaría : Please check now Commented Nov 12, 2014 at 6:49
  • have you debug your code?in which line it gives this error? Commented Nov 12, 2014 at 6:56
  • check the value if Date_Borrowed and Date_Returned. What is data type of those column in database table? Commented Nov 12, 2014 at 6:56
  • @yogi970 : im getting error on this line " int Copy_Id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[1]);" Commented Nov 12, 2014 at 6:58

3 Answers 3

1

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>
Sign up to request clarification or add additional context in comments.

Comments

0

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);

7 Comments

i have done the changes on above mentioned fields. Now im getting same error whic got previously for " DateTime Date_Borrowed = Convert.ToDateTime(GridView1.DataKeys[e.RowIndex].Values[2]) ; DateTime Date_Returned = Convert.ToDateTime(GridView1.DataKeys[e.RowIndex].Values[3]);" lines
now you are not getting error on copy_id field? if no then change code for other field also as i mentioned in "Recommendation"!
Oh you are using date picker.Try to make txtDateReturned as "ReadOnly". If it works then do comment here
im geeting error for ".Text" [web.UI.Control does not contain definition for 'Text']
is this error in "txtDateReturned" textbox only,other then this all other textboxes and dropdown giving values properly? also check your "txtDateReturned" in source. i think you have miss spell the Test property to "sText". just check it
|
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.