0

I got a chance to work in the migration project(VB to VB.NET).I was confusing in the RecordSet functionality.In our project we are taking the DataReader according to the scenario.But in the Looping cases like dsr.EOF.

  Dim recordset As dao.RecordSet
If recordset .EOF Then
       '    '    msgbox "Please enter some number ", vbOKOnly, "Number Not Found"
       '    '    txtAdd.SetFocus
       '    '    Exit Sub
End If

How to convert this into VB.NET? Either we need to write If Not reader.Read or If reader.Read.

Can any help me regarding this ?

Hemant Kumar
  • 4,593
  • 9
  • 56
  • 95

1 Answers1

1

I think what you might be after is the following

Dim reader As IDataReader
' <fill the reader with your query via an IDbCommand>
Dim dataFound As Boolean = false

Using reader = command.ExecuteQuery()
    While reader.Read()
        ' <perform your data processing here>
        dataFound = True
    End While

    If dataFound = False Then
        ' <Here you do your processing for the case where data was not found.>
    End If
End Using

Or if you are only after one row of data, then we have:

Dim reader As IDataReader
' <fill the reader with your query via an IDbCommand>

Using reader = command.ExecuteQuery()
    If reader.Read() Then
        ' <perform your data processing here>
    Else
        ' <Here you do your processing for the case where data was not found.>
    End While
End Using
Miika L.
  • 3,333
  • 1
  • 24
  • 35
  • You mean use "If" rather than the "While" loop? Well, yes, if you were only after one row of data, but then you should change it to be "If reader.Read() Then Else End If" – Miika L. Apr 26 '12 at 10:19
  • Added the single row solution to the answer also. – Miika L. Apr 26 '12 at 10:21
  • I have one more Question regarding this that If Not (Not recordSet.EOF) Then 'Code of Lines How to write this in VB.NET?I tried like this If Not reader.Read Is this right? – Hemant Kumar Apr 26 '12 at 10:23
  • "If Not (Not recordSet.EOF)" is the same as "If recordSet.EOF", which in Vb.Net would be equivalent to "If Not reader.Read()", yes. However, keep in mind that once you have made the reader.Read() call, you can't return to the previous row. – Miika L. Apr 26 '12 at 10:26
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/10540/discussion-between-steve-rose-and-miika-l) – Hemant Kumar Apr 26 '12 at 10:30