Bug #33504 Problem accesing Datareader with MySQL .NET Connector 5.1.4
Submitted: 24 Dec 2007 20:27 Modified: 2 Feb 2008 21:34
Reporter: Teofilo Homsany Email Updates:
Status: No Feedback Impact on me:
None 
Category:Connector / NET Severity:S1 (Critical)
Version:5.1.4 OS:Any
Assigned to: CPU Architecture:Any
Tags: .net, DataReader, problems

[24 Dec 2007 20:27] Teofilo Homsany
Description:
Hi!!
I have been trying to fix an error I keep getting on my VB.NET code.
For some reason, I get an error saying "Invalid attempt to access a field 
before calling Read()" everytime. As you can see in my code below, I created 
the connection, connection string, etc and I opened the connection, executed 
the reader and then called the datareader.read() before any variable 
assignment below.
I keep getting that error and I don't know why! I am going crazy.
Please check my code below:

\\

Dim login As String = txtlogin.Text
Dim pass As String = txtpass.Text

Dim strConn As String
strConn = "host=201.218.239.10; user id=acarsuser; password=xxxxx; 
database=acarssystem"
Dim conn As New MySqlConnection(strConn)
Dim SQLCode As String
SQLCode = "SELECT DISTINCT pilot_id,name, password FROM pilot WHERE name='" 
+ login + "' AND password = '" + pass + "'"
Dim datareader As MySqlDataReader
Dim Cmd As New MySqlCommand(SQLCode, conn)

Try
conn.Open()
datareader = Cmd.ExecuteReader

datareader.Read()

Dim userID As String = datareader("name").ToString
Dim userpassword As String = datareader("password").ToString
If userID = login And userpassword = pass Then
'Creo la conexion para notificar al DB que el piloto esta Online
Dim pilotID As Integer
pilotID = datareader("pilot_id").ToString
Dim strConn1 As String
strConn1 = "host=201.218.239.10; user id=acarsuser; password=xxxxx; 
database=acarssystem;PORT=3306"
Dim conn1 As New MySqlConnection(strConn1)
Dim SQLCode1 As String
SQLCode1 = "INSERT INTO acars_pilot (pilot_id) VALUES (" + pilotID + ")"
Dim myCommand As MySqlCommand
conn1.Open()
myCommand = New MySqlCommand(SQLCode1, conn1)
myCommand.ExecuteNonQuery()
conn1.Close()
Me.Hide()
AcarsMain.Show()
Else
'Aviso que el usuario no existe
MsgBox("The user and password you supplied is not on the system. Please try 
again.", MsgBoxStyle.Exclamation)
End If

conn.Close()

Catch ex As Exception
MsgBox("Connection to ACARS server Unsuccessfull. Please try again later." & 
ex.Message, MsgBoxStyle.OkOnly)

End Try
\\

For some reason the data reader doesn't work at all.No records are returned 
and I get no message as I have in the code.
Any suggestions?
Thanks,
Teo 

How to repeat:
Create both textboxes and the submit button. It is not working at all.

Suggested fix:
None at the time.
[2 Jan 2008 21:34] Reggie Burnett
Are you sure that your query should return data?  If not you still should not getting that error but that might help us locate it.
[3 Feb 2008 0:00] Bugs System
No feedback was provided for this bug for over a month, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
[3 Apr 2008 2:33] Junior Zegarra
A mi me paso lo mismo.

El tema es el "DISTINCT".
Cuando ejecuto SELECT Att, Att1, Att2... me devuelve registros - OK
Pero si ejecuto SELECT DISTINCT Att, Att1, Att2 ... NO me devuelve registros - PROBLEM.

Lo que hice para solucionar fue quitar una Columna de "TIPO FECHA" y luego probe y todo estaba bien!.

Saludos

Junior J. Zegarra

Version MySql 5.1.23
[9 Apr 2008 14:25] Reggie Burnett
Junior

I'm sorry but you will need to write in English here.