locked
System.IndexOutOfRangeException RRS feed

  • Question

  • Hello:  I have a problem that I've been trying to resolve all day while debugging in vb.  I am a beginner, but I can figure it out if someone can point me in the right direction.  I'm sure it is simple, but I have yet to find an answer.

    Here's what my environment looks like:

    Project = Web Application
    Current Dev = VS '08 w/ Framework 3.5 SP1 in VBasic
    Orig Proj Built = VS '05 w/ Framework 1.1
    dBase = SQL Server '05

    Problem = System.IndexOutOfRangeException was unhandled by user code
      Message="There is no row at position 0."
      Source="System.Data"

    Already Tried = SQL While... Try - Catch statement, but stored proc has SELECT w/ 2 parameters instead of UPDATE or INSERT statements so I'm not sure how to write code for that.  Also, I converted C # code to VB using other posts online similar to my problem, but alas no solution yet.

    I do know that the stored proc is not returning a value for either param [in the SELECT] because there is no row in the table to return to the dataset.  Anyhow, here's my code.

    Sequence of Events = Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Page_Load calls Private Sub DisplayData()
    DisplayData() then calls Private Sub ValidateAdvancePaymentAmount() 

    ----- Begin Code for Private Sub ValidateAdvancePaymentAmount() -----

    Private Sub ValidateAdvancePaymentAmount()
                ' 05.14.09: Moved this Dim further down in the code - hmorgan
                'Dim _PaymentRequestBL As New PaymentRequestBL
    
                'server validator for amount, client did not work for nummeric data field
                CVRequestAmount.IsValid = True
    
                ' 05.14.09 Added validation when the Request Advance has been approved by Grant Adjustment
                ' and the CurrentReaderRequestAdvancePageOpen.GetInt("ReturnValue") = 2
                Dim _GrantStatusBL As New GrantStatusBL
                Dim RequestAdvancePageOpen As DataSet = _GrantStatusBL.IsRequestAdvancePageOpen(currentGrantNumber)
                Dim CurrentReaderRequestAdvancePageOpen As New drReader(RequestAdvancePageOpen.Tables(0).Rows(0))
    
                If CurrentReaderRequestAdvancePageOpen.GetInt("ReturnValue") = 2 Then
                    CVRequestAmount.IsValid() = True
                    Return '<<--
                Else
                    ' 05.22.09: Added validation to test why System.IndexOutOfRangeException is firing on [proc_CheckAdvancePaymentOverride] - hmorgan
                    CVRequestAmount.IsValid() = False
    
                End If
    
                Dim _PaymentRequestBL As New PaymentRequestBL
                Dim TestString As String = ("0" + Me.RequestAmount.Text).Replace(",", "").Replace("$", "")
                Dim TestString2 As String = "0"
    
                If ViewState("MaximumApprovedAmmountCanRequest") Is Nothing Then
                    TestString2 = ("0" + Me.MaxmumAmountCanRequest.Text).Replace(",", "").Replace("$", "")
                Else
                    TestString2 = ("0" + ViewState("MaximumApprovedAmmountCanRequest")).Replace(",", "").Replace("$", "")
                End If
    
                Dim DSCurrent As DataSet = _PaymentRequestBL.CheckAdvancePaymentOverride( _
                    currentGrantNumber)
    
                ' 05.22.09: Added Code to Catch Errors when Table has no rows for System.Index.OutOfRangeException block - hmorgan
                If DSCurrent.Tables(0).Rows(0) Is Nothing Then
                    Message.Text = GetSimpleDisplayText(25628)
                Else
                    If Not DSCurrent.Tables(0).Rows(0)("RequestAdvanceAmount") Is System.DBNull.Value AndAlso _
                             DSCurrent.Tables(0).Rows(0)("isAmountOverride") = True Then
                        If Convert.ToDouble(TestString) > Convert.ToDouble(DSCurrent.Tables(0).Rows(0)("RequestAdvanceAmount")) Or Convert.ToDouble(TestString) <= 0 Then
                            CVRequestAmount.IsValid = False
    
                            ' 05.14.09: Added CV to code instead of firing CustomValidator in HTML - hmorgan
                            Message.Text = GetSimpleDisplayText(25627)
                        End If
                    Else
                        If Convert.ToDouble(TestString) > Convert.ToDouble(TestString2) Or Convert.ToDouble(TestString) <= 0 Then
                            CVRequestAmount.IsValid = False
    
                            ' 05.14.09: Added CV to code instead of firing CustomValidator in HTML - hmorgan
                            Message.Text = GetSimpleDisplayText(25627)
    
                        End If
                    End If
                End If
    


    ----- End Form Private Sub -----

    Here is where the problem occurs - look at the nested If statements after the comment (as shown above) starting with " 05.22.09: Added Code to Catch Errors..." - If DSCurrent.Tables(0).Rows(0) Is Nothing Then

    ----- Begin Exception Block -----

    System.IndexOutOfRangeException was unhandled by user code
      Message="There is no row at position 0."
      Source="System.Data"
      StackTrace:
           at System.Data.RBTree`1.GetNodeByIndex(Int32 userIndex)    at System.Data.RBTree`1.get_Item(Int32 index)    at System.Data.DataRowCollection.get_Item(Int32 index)    at eGrants.Web.RequestAdvance.ValidateAdvancePaymentAmount() in C:\Visual Studio\Visual Studio Projects\eGrants\Inetpub\wwwroot\eGrants\Project\requestadvance.aspx.vb:line 596    at eGrants.Web.RequestAdvance.DisplayData() in C:\Visual Studio\Visual Studio Projects\eGrants\Inetpub\wwwroot\eGrants\Project\requestadvance.aspx.vb:line 209    at eGrants.Web.RequestAdvance.Page_Load(Object sender, EventArgs e) in C:\Visual Studio\Visual Studio Projects\eGrants\Inetpub\wwwroot\eGrants\Project\requestadvance.aspx.vb:line 70    at System.Web.UI.Control.OnLoad(EventArgs e)    at System.Web.UI.Control.LoadRecursive()    at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
      InnerException: 
    

    ----- End Exception Block -----

    I've included the Stored Procedure as well just in case I didn't include enough info in this post.

    ----- Begin SQL Stored Procedure -----

    set ANSI_NULLS ON
    set QUOTED_IDENTIFIER ON
    go
    
    /* Programmer Change Log Notes */
    
    ALTER Procedure [dbo].[proc_CheckAdvancePaymentOverride]
    
    /* I replaced the param default value with a dummy value */
    
    	@GrantNumber VARCHAR(50) = '0000000'
    AS
    	DECLARE @GrantID VARCHAR(50)
    	DECLARE @GrantCountID VARCHAR(2)
    
    	SET  @GrantID = SUBSTRING(@GrantNumber,1,5)
    	SET  @GrantCountID = SUBSTRING(@GrantNumber,6,2)
    
    	SELECT ISNULL(AmountOverride,0) As isAmountOverride, ISNULL(RequestAdvanceAmount,0) As RequestAdvanceAmount
    	FROM GrantAdjustment 
    	WHERE GrantID =@GrantID and GrantCountID=@GrantCountID
    	and AdjustmentID IN (SELECT Max(AdjustmentID) FROM GrantAdjustment where GrantID =@GrantID and GrantCountID=@GrantCountID)

    ----- End SQL Stored Procedure -----

    Sincerely,

    Heather Morgan

     

    Saturday, May 23, 2009 12:55 AM

Answers

  • Roahn:  I appreciate your quick response.

    re: EXEC proc_CheckAdvancePaymentOverride - no data is returned from this proc for either isAmountOverride or RequestAdvanceAmount columns.

    In laymans terms, what I cannot figure out is how to code the aspx.vb Form when the dataset is returned empty.  That's what my real problem is.  Thanks!
    • Marked as answer by dipitiduda Monday, May 25, 2009 3:54 PM
    Monday, May 25, 2009 1:24 PM

All replies

  • Hello,

    This Exception (System.IndexOutOfRangeException) occurred because the DataSet DSCurrent got no data(s). I assume we called the stored procedure by using
            Dim DSCurrent As DataSet = _PaymentRequestBL.CheckAdvancePaymentOverride(currentGrantNumber)
    Why not test the stored procedure (named dbo.proc_CheckAdvancePaymentOverride in this case) in SQL Server by executing
            exec [dbo].[proc_CheckAdvancePaymentOverride] '<currentGrantNumber>' 
    Does it return any data?

    By the way, we could find more information on Execute Command by clicking: http://msdn.microsoft.com/en-us/library/ms188332.aspx

    Best regards,
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    If you have any feedback, please tell us.
    Welcome to the All-In-One Code Framework!
    Monday, May 25, 2009 7:19 AM
  • Roahn:  I appreciate your quick response.

    re: EXEC proc_CheckAdvancePaymentOverride - no data is returned from this proc for either isAmountOverride or RequestAdvanceAmount columns.

    In laymans terms, what I cannot figure out is how to code the aspx.vb Form when the dataset is returned empty.  That's what my real problem is.  Thanks!
    • Marked as answer by dipitiduda Monday, May 25, 2009 3:54 PM
    Monday, May 25, 2009 1:24 PM
  • OK, I found out the answer:  I simply had to handle the Exception where Dataset returns NOTHING.  Very simple code obviously, but I'm a beginner.  Thanks!
    Monday, May 25, 2009 3:57 PM