.NET error end of statement expected

Posted on 2012-08-19
Last Modified: 2012-08-19
Im getting the errors in the while loop on lines 22 & 23
dr[FirstName].ToString() is underlined blue
and the one under it as well the same thing

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim db As New databaseConnection()
        Dim comboText As String
        Dim comboValue As String
        Dim myArrayList As New ArrayList
        Dim cmd As New MySqlCommand
        Dim sqlQuery As String = "SELECT * FROM customers WHERE"
        If searchBy <> "" Then
            sqlQuery += " " & searchBy & "='" & TextBox1.Text & "' and"
        End If
        sqlQuery = sqlQuery & " merchantId=" & Form1.mMerchantId
        ' MessageBox.Show(sqlQuery)
        cmd.CommandText = sqlQuery

        Dim dr As MySqlDataReader = db.DataReader(cmd)

        If dr.HasRows() Then
            While dr.Read()
                comboText = ""
                comboValue = ""
                comboText = dr[FirstName].ToString() 
                comboValue = dr[customerId].ToString()
                myArrayList.Add(New ComboBoxValues(comboText, comboValue))

                ComboBox4.DataSource = myArrayList
                ComboBox4.DisplayMember = "DisplayMember"
                ComboBox4.ValueMember = "ValueMember"
            End While
        End If
    End Sub

Open in new window

Question by:prowebinteractiveinc
    LVL 16

    Expert Comment

    by:Ady Foot
    Does the FirstName field definitely exist within the data table?  That would be the obvious problem without looking in too much detail.

    Author Comment

    there is no way for the program to know that. FirstName comes from a mysql Database
    Im used to using dr(0)

    I also just encountered another error. my datareader is only picking up one result. where I know it should be returning more then one ?
    LVL 69

    Expert Comment

    by:Éric Moreau
    you are implicitly calling the Item property of the DataReader object. It can only takes an integer in parameter. Check
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    SQUARE brackets are used in C#.

        comboText = dr[FirstName].ToString();

    You need PARENTHESIS:

        comboText = dr(FirstName).ToString()

    Author Comment

    so how can I define the field name instead ?
    LVL 85

    Accepted Solution

    *If those are the actual column names then you need a string literal too:

        comboText = dr("FirstName").ToString()

    Author Comment

    idle_Mind, I tried what you mentioned it didnt work - gave a bigger error...
    LVL 69

    Expert Comment

    by:Éric Moreau
    >>so how can I define the field name instead ?

    not possible with a DataReader. You will need to use a DataTable instead

    If you want to keep the DataReader, change SELECT * for SELECT customerId, FirstName so that you can use 0 and 1 in parameters. Using Select * is not in the best practices anyway

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Normally the drop down box control found in the .Net framework tools is able to select just one data and value at a time, which is displayed on the text area.   But what if you want to have multiple values to be selected in the drop down box? As …
    The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now