Solved

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

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

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Hangfire / asp.net sample 3 28
creating a flowchart from an algorithm 5 22
Wpf develop 5 32
Hide Tab Page 3 19
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now