how to get current row index when reading DbDataReader in ADO.net

591 views Asked by At

i have an excel sheet which i read with a code like this

Public Async Function ReadRecordAsync() As Task(Of SellingList)
        Try
            Dim Sellings As New SellingList
            Await Conn.OpenAsync()
            Cmd = New OleDbCommand()
            Cmd.Connection = Conn
            Cmd.CommandText = "Select * from [Sheet1$]"
            Dim Reader = Await Cmd.ExecuteReaderAsync()
            While Reader.Read()
                If CheckEmptyReader(Reader) Then
                    MsgBox("First Empty Cell Index = " & Reader.Depth)
                    Exit While
                End If
                Dim x As New BuyableObject
                If Not Reader.IsDBNull(0) Then
                    x.Name = Reader.GetString(0)
                End If
                If Not Reader.IsDBNull(1) Then
                    x.SoldCount = GetIntegerValue(Reader(1).ToString)
                End If
                If Not Reader.IsDBNull(2) Then
                    x.SellingPrice = GetSingleValue(Reader(2).ToString)
                End If
                If Not Reader.IsDBNull(3) Then
                    x.OriginalPrice = GetSingleValue(Reader(3).ToString)
                End If
                x.UpdateCounts()
                If x.Name IsNot Nothing Then
                    Sellings.Add(x)
                End If
            End While
            Reader.Close()
            Conn.Close()
            Return Sellings
        Catch ex As Exception
            MsgBox(ex.ToString)
            Return New SellingList
        End Try
    End Function

and i want to get the current row index at

MsgBox("First Empty Cell Index = " & Reader.Depth)

but it doesn't work and always returns 0 and to check empty reader :

Public Function CheckEmptyReader(reader As DbDataReader) As Boolean
        If IsDBNull(reader(0)) AndAlso IsDBNull(reader(1)) AndAlso IsDBNull(reader(2)) AndAlso IsDBNull(reader(3)) Then
            Return True
        Else
            Return False
        End If
    End Function

so how do i get the current row index with the reader ?

1

There are 1 answers

0
Ahmed Fwela On

i found a simple solution by adding a simple counter

Dim counter As Integer = 1 'start index
While Reader.Read()
   If CheckEmptyReader(Reader) Then
      MsgBox("First Empty Row Index = " & counter)
      Exit While
   End If
   'some code
   counter += 1
End While