Kismet Agbasi
Kismet Agbasi

Reputation: 567

What's Causing My If Statement To Be Skipped?

I have an embedded IF statement that should execute following a Database query. However, I noticed at run-time that the entire statement is not being evaluated at all (the one right after While dbData.Read()). What am I doing wrong?

HERE'S THE CODE:

Private Sub ButtonNew_Click(ByVal sender As System.Object, ByVal e As     System.EventArgs) Handles ButtonNew.Click

If TextBoxSearch.Text = "" Then
        MessageBox.Show("Sorry, you must enter an ACCOUNT# before proceeding!")
        TextBoxSearch.Focus()
    Else
        Try
            Dim dbConn As MySqlConnection
            dbConn = New MySqlConnection("Server=" & FormLogin.ComboBoxServerIP.SelectedItem & ";Port=3306;Uid=parts;Password=parts;Database=accounting")
            Dim account As Boolean = True
            If dbConn.State = ConnectionState.Open Then
                dbConn.Close()
            End If
            dbConn.Open()
            Dim dbQuery As String = "SELECT * FROM customer WHERE accountNumber = '" & TextBoxSearch.Text & "';"
            Dim dbData As MySqlDataReader
            Dim dbAdapter As New MySqlDataAdapter
            Dim dbCmd As New MySqlCommand
            dbCmd.CommandText = dbQuery
            dbCmd.Connection = dbConn
            dbAdapter.SelectCommand = dbCmd
            dbData = dbCmd.ExecuteReader
            While dbData.Read()
                If dbData.HasRows Then
                    'MessageBox.Show("Customer record already exists!")
                    Call recordExists()
                    account = False
                    Call lockForm()
                    TextBoxSearch.Focus()
                    Me.Refresh()
                Else
                    'dbData.Close()
                    account = True
                    TextBoxAccount.Text = TextBoxSearch.Text
                    TextBoxAccount.BackColor = Color.LightGray
                    TextBoxAccount.TabStop = False
                    Call unlockForm()
                    TextBoxLastName.Focus()
                    ButtonSubmit.Visible = True
                    Me.Refresh()
                End If
            End While
            dbData.Close()
            dbCmd.Dispose()
            dbAdapter.Dispose()
            dbConn.Close()
        Catch ex As Exception
            MessageBox.Show("A DATABASE ERROR HAS OCCURED" & vbCrLf & vbCrLf & ex.Message & vbCrLf & _
                        vbCrLf + "Please report this to the IT/Systems Helpdesk at Ext 131.")
        End Try
    End If

End Sub

Upvotes: 0

Views: 107

Answers (1)

Steven Doggart
Steven Doggart

Reputation: 43743

If dbData.Read() returns False, then it will not enter your loop and therefore the If statement will not be executed. If there are no rows to be read, then Read always returns False, so the If statement is useless where it's at. You need to move that If statement up so that the while loop is inside the If block:

If dbData.HasRows Then
    While dbData.Read()
        ' ...
    End While
Else
    ' ...
End If

Upvotes: 1

Related Questions