Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2010-11-09
5
Medium Priority
?
415 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 400 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 1200 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 400 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

618 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