Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8689309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:36:49+00:00 2026-06-12T23:36:49+00:00

I have an embedded IF statement that should execute following a Database query. However,

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T23:36:51+00:00Added an answer on June 12, 2026 at 11:36 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Java application that uses an Apache Derby database with the embedded
I have the following statement that is executed at the end of a really
I have embedded a JTextArea on a JScrollPane and am using that JTextArea for
I have the following problem. I have embedded a map using Google Maps' API.
In my Java code I have embedded a SQL query which fetches data from
I have an embedded database in windows forms app. Is it possible to browse
I have several embedded linux systems that I want to write a 'Who's Online?'
On an embedded type system, I have created a Small Object Allocator that piggy
I have a code that runs on an embedded system and it has to
I have a document Design.rb that has an embedded document Attachment.rb that does some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.