Solved

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

Posted on 2010-11-09
5
408 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
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Gridview selected row 9 47
imap mails 1 24
REXEX help Part 2 2 25
VB.net capture  result of a SQL query in variables 3 12
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
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…

803 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