none
Idatareader GetOrdinal IndexOutOfRange exception RRS feed

  • Question

  • This is the error I am getting. I have similar kind of errors getting on many other pages in  my application. Can anyone help me if there is anything wrong in the structure of the code , or at what point the problem is occuring.

    Exception of type 'System.Web.HttpUnhandledException' was thrown.
    pKey
       at System.Data.ProviderBase.FieldNameLookup.GetOrdinal(String fieldName)
       at System.Data.SqlClient.SqlDataReader.GetOrdinal(String name)
       at System.Data.SqlClient.SqlDataReader.get_Item(String name)
       at ReadyCollect.CaseDetail.m_FillUserArray()
       at ReadyCollect.CaseDetail.Page_Load(Object sender, EventArgs e)
       at System.Web.UI.Control.OnLoad(EventArgs e)
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

    I am getting this error is following code fragment.

    Public iUsrArrCnt As Integer = 0 
     
    Public myUsrArr(999, 99) As String 
     
    Sub m_FillUserArray() 
     
           
    Dim X As Integer = 1 
           
    Dim sPKey As String = "" 
           
    Dim sDesc As String = "" 
           
    Dim DAl As New DataAccessLayer 
     
     
            dbReader
    = DAl.GetUsersOrderPKey(ClientKey) 
           
    If Validation.Validate_DataReader(dbReader) Then 
               
    While dbReader.Read 
                        sPKey
    = "" 
                    sPKey
    = DB_Objects.Convert_objToString(dbReader.Item("pKey")) 
                    sDesc
    = "" 
                    sDesc
    = DB_Objects.Convert_objToString(dbReader.Item("UserName")) 
     
                myUsrArr
    .SetValue(sPKey, X, 1) 
                        myUsrArr
    .SetValue(sDesc, X, 2) 
     
                X
    += 1 
     
               
    End While 
           
    End If 
           
    Validation.Close_DataReader(dbReader) 
     
            iUsrArrCnt
    = X - 1 
     
       
    End Sub 

    The data access method that is called is below.

     'retreives all records from the table users and order it by pkey Public Function

     

    GetUsersOrderPKey(ByVal ClientKey As Integer) As System.Data.IDataReader

    Dim db As Database = DatabaseFactory.CreateDatabase()

    Dim sqlCommand As String = "USP_DISPLAYUSERSorderPKEY"

    Dim dbCommand As DbCommand = db.GetStoredProcCommand(sqlCommand)

    db.AddInParameter(dbCommand, "ClientKey", DbType.Int32, ClientKey)

    Return db.ExecuteReader(dbCommand)

     

    This DAL method triggers the stored procedure

    GO

    /** Object: StoredProcedure [dbo].[USP_DISPLAYUSERSORDERPKEY] Script Date: 09/21/2010 13:28:45 **/

    SET ANSI_NULLS ON

    GO

    SET QUOTED_IDENTIFIER ON

    GO

    ALTER PROCEDURE [dbo].[USP_DISPLAYUSERSORDERPKEY]

    @ClientKey int

    AS

    BEGIN

    SELECT * FROM [Users] WHERE [ClientKey]=@ClientKey ORDER BY [pKey]

    END

    End Function

    Tuesday, September 21, 2010 6:02 PM

Answers

  • The error occurs somewhere in your code where it gall GetOrdinal method of the DataReader. I do not see anything in your posted code that uses this method, but error happens in a case if code tries to access column in DataReader with the index that does not exist. For example, if your query returns 5 columns of data, but code tries to access data using column with index 5 (sixth column) you would receive that error.


    Val Mazur (MVP) http://www.xporttools.net
    • Marked as answer by liurong luo Wednesday, September 29, 2010 4:55 AM
    Wednesday, September 22, 2010 10:23 AM
    Moderator

All replies

  • The error occurs somewhere in your code where it gall GetOrdinal method of the DataReader. I do not see anything in your posted code that uses this method, but error happens in a case if code tries to access column in DataReader with the index that does not exist. For example, if your query returns 5 columns of data, but code tries to access data using column with index 5 (sixth column) you would receive that error.


    Val Mazur (MVP) http://www.xporttools.net
    • Marked as answer by liurong luo Wednesday, September 29, 2010 4:55 AM
    Wednesday, September 22, 2010 10:23 AM
    Moderator
  • Hello,

    Based on the documentation described here:
    http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.getordinal.aspx

    Please check if there are any records in your datareader.
    "The method throws an IndexOutOfRange exception if the zero-based column ordinal is not found."

    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!
    Friday, September 24, 2010 4:39 AM