Ado Executereader

brucelim80

Active member
Joined
Apr 20, 2006
Messages
35
Programming Experience
Beginner
Hi all,

I have the following code for adding item into the checkbox with data extracted from the database.

However, when i removed the line in RED, the while loop show one item less such that the first record is missing. Does anyone know any proper code for this issue.

Thank you



Private Sub Dataentry_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim myConnects As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\136.121.2.1\its helpdesk\calltrack\MDB\ecall.mdb;")
Dim strSelect As New OleDbCommand("SELECT deptcode FROM DeptDB ", myConnects)


myConnects.Open()
Dim reader1 As OleDbDataReader = strSelect.ExecuteReader
With reader1
If .Read Then
departtxt.Items.Add(.Item("deptcode").ToString)
While .Read
departtxt.Items.Add(.Item("deptcode").ToString)
End While
End If
End With
 
You do not need that entire if block.

The read function returns a boolean indicating whether or not a record was read into memory. Therefore when you say "if .read() then " this is moving to the first record. If you take this if out and the line in red and just let the loop run this should fix the issue.
 
The read function would then evaluate to false and the while block will never execute. If the first iteration of a while block results in false it will never run.

All of these loops will never run:
VB.NET:
        Dim blResult As Boolean = False

        While blResult
            MsgBox("Good luck getting here.")
        End While
        
        Do Until blResult = False
            MsgBox("This should never run.")
        Loop

        Do While blResult
            MsgBox("Never")
        Loop
 
Thank for your respond ss7thirty.
But if i take out the if line. What will happen if there is no data found?

What do you think is different between a WHILE and an IF

Think about it!

IF (test) THEN
do block of code once
END

WHILE (test) THEN
do block of code at least once, maybe more
END


While and If are no different in cases where test is FALSE, or TEST is only true once!
 
Back
Top