Solved

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

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

Get Actionable Data from Your Monitoring Solution

Your communication platform is only as good as the relevance of the information you send. Ensure your alerts get to the right people every time with actionable responses. Create escalation rules that ensure everyone follows the process and nothing is left to chance.

Question has a verified solution.

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

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…
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…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

719 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