Link to home
Start Free TrialLog in
Avatar of cliron
clironFlag for Ireland

asked on

MySQLCommand Returns no values

I have the following code in a form that tries to take data from two tables connected by a LEFT JOIN and write the results to a text file. If I run the query in MySQL Query Browser it works as expected. For test purposed only I ran the query selectying data from 1 table only (LEFT JOIN redundant I know but trying to see if there was a syntax issue) and it worked as expected. Am I missing something in trying to run this query from inside a VBasic Express form.

       Try
            Dim myQuery As String
            myQuery = "SELECT utName, utRating, ulTotSeconds FROM usrtime u "
            myQuery = myQuery & "LEFT JOIN usrList l "
            myQuery = myQuery & "ON u.utName = l.nameWeb "
            myQuery = myQuery & "WHERE ASCII(u.utName) > 64 "
            myQuery = myQuery & "ORDER BY u.utName"

            Dim myConn As New MySqlConnection(connStr)
            Dim myCmd As New MySqlCommand(myQuery, myConn)

            myConn.Open()

            Dim myReader As MySqlDataReader
            myReader = myCmd.ExecuteReader()

            While myReader.Read()
                objWrite.WriteLine((myReader.GetString(0) & ", " & _
                    myReader.GetString(1) & ", " & myReader.GetString(2)))
            End While

            myReader.Close()
            myConn.Close()
            objWrite.Close()

Open in new window

Avatar of Michael701
Michael701
Flag of United States of America image

Why are you doing the LEFT JOIN if you're only using columns from usrtime?

myQuery = "SELECT utName, utRating, ulTotSeconds FROM usrtime "
            myQuery = myQuery & "WHERE ASCII(utName) > 64 "
            myQuery = myQuery & "ORDER BY utName"
next check for errors after the ExecuteReader()

should: ulTotSeconds be utTotSeconds to keep with the naming conventions? Could this be causing the empty data set?
Avatar of cliron

ASKER

As I said I only took values from 1 table to make sure there was nothing wrong with the syntax. The problem arises when I try and take values from 2 tables.
Avatar of cliron

ASKER

ulTotSeconds is a value from the 2nd table (ut = usrTime, ul = usrList)
Avatar of Tomas Helgi Johannsson
           Hi!

Try this
ON TRIM(u.utName) = TRIM(l.nameWeb)

If this is the case you have in either column of these tables values with blanks that can interfere with the result you want.
And you would need to correct those values.

Also check if these joins would help in the same way
UPPER(u.utName) = UPPER(l.nameWeb)
or
TRIM(UPPER(u.utName)) = TRIM(UPPER(l.nameWeb))

Hope this helps.

Regards,
     Tomas Helgi
Avatar of cliron

ASKER

Tried that but still no joy. I have employed the same query but outputted to a datagrid view and it works as expected. There must be something quirky with the executereader.

Thanks.
ASKER CERTIFIED SOLUTION
Avatar of Tomas Helgi Johannsson
Tomas Helgi Johannsson
Flag of Iceland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cliron

ASKER

MySQLException threw up a data is null error message. Have got around the issue with including an IFNULL(fld1,fld2) expression and the Execute reader is now working as expected.

Thanks.