Solved

Index was out of range.  DataKeys(GridView1.EditIndex).Value

Posted on 2010-11-09
5
411 Views
Last Modified: 2012-05-10
In the VB behind a GridView I've got the DataBound method below.

The .aspx page loads as long as a row happens to already be in Edit mode, but if I click Cancel or Save, I get:
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
on row 2 below.
It seems that the "EditIndex" is Nothing when it's not in Edit mode and throwing the out of range error.  How can I avoid the error when the GridView is not in Edit mode?
Protected Sub GridView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.DataBound
    Dim iReqID As Integer = CType(GridView1.DataKeys(GridView1.EditIndex).Value, Integer)        'CInt(GridView1.DataKeys(GridView1.EditIndex).Value)
    Dim iModifiedBy As String = iUserName
    Call SendModifiedByToRequisition(iModifiedBy, iReqID)
End Sub

Open in new window

0
Comment
Question by:megnin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 20

Assisted Solution

by:Daniel Van Der Werken
Daniel Van Der Werken earned 100 total points
ID: 34093227
Can you put the code that executes in an "if" statement?

I'm not a VB.NET person, but it would be something like this:

if GridView1.EditIndex != Nothing then
   Dim iReqID = ...etc.
   Dim iModifiedBy = etc.
   Call etc..
Else -- don't do anything..

0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 300 total points
ID: 34093233
Actualy EditIndex should be -1 if not in Edit mode, so you could try:
Protected Sub GridView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.DataBound
    If GridView1.EditIndex <> -1 Then
        Dim iReqID As Integer = CType(GridView1.DataKeys(GridView1.EditIndex).Value, Integer)        'CInt(GridView1.DataKeys(GridView1.EditIndex).Value)
       Dim iModifiedBy As String = iUserName
       Call SendModifiedByToRequisition(iModifiedBy, iReqID)
    End If
End Sub
 

Open in new window

0
 
LVL 1

Author Comment

by:megnin
ID: 34093255
Oh, that's right.  The EditIndex is -1 when it's in Edit Mode.  
0
 
LVL 32

Assisted Solution

by:Erick37
Erick37 earned 100 total points
ID: 34093284
If GridView1.EditIndex IsNot Nothing AndAlso GridView1.EditIndex > -1 Then
...
0
 
LVL 1

Author Closing Comment

by:megnin
ID: 34093310
Thank you, all thee.  Carl's solution was exactly right.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question