Reputation:
I am sorry for my bad english. I hope somebody could explain what the error The conversion from type MySqlDataReader to type Integer is invalid
means.
I was sure that it was a simple 'Dim a Integer, but It is larger than that, or I am just been stupid.
My btnLogin_Click code:
Dim SQL As New MySQLHook
Dim loginResult As Integer
Dim User As TextBox = Me.Controls.Item("TextGebruikersNaam")
Dim Pass As TextBox = Me.Controls.Item("TextGebruikersPass")
If String.IsNullOrEmpty(User.Text) Or String.IsNullOrEmpty(Pass.Text) Then
MsgBox("Voer een gebruikersnaam en wachtwoord in om verder te gaan.", MsgBoxStyle.Exclamation, "Foutmelding")
User.Focus()
Else
loginResult = SQL.Results("SELECT * FROM tblgebruikers " & "WHERE GebruikersNaam='" & User.Text & "' AND " & "GebruikersPass='" & Pass.Text & "'")
If loginResult = 1 Then
' Login was good, todo: add code here
Else
MsgBox("De gebruikersnaam of wachtwoord is onjuist. Probeer het opnieuw of vraag een nieuw wachtwoord aan.", MsgBoxStyle.Exclamation, "Foutmelding")
User.Text = ""
Pass.Text = ""
User.Focus()
End If
End If
My Results code (SQL.Results is from As New MySQLHook)
Public Function Results(ByVal sql)
Try
Dim conn = Connect()
Dim myAdapter As New MySqlDataAdapter
Dim myCommand As New MySqlCommand()
myCommand.Connection = conn
myCommand.CommandText = sql
myAdapter.SelectCommand = myCommand
Dim myData As MySqlDataReader
myData = myCommand.ExecuteReader()
Return myData
Catch myerror As MySqlException
clsError.logMessage("De server retourneerde de volgende foutmelding: " & myerror.Message)
Return DBNull.Value
End Try
End Function
I olso tried using this function but it gave me the same error message:
Public Function num_results(ByVal sql)
Try
Dim conn = Connect()
Dim myAdapter As New MySqlDataAdapter
Dim myCommand As New MySqlCommand()
myCommand.Connection = conn
myCommand.CommandText = sql
myAdapter.SelectCommand = myCommand
Dim myData As MySqlDataReader
myData = myCommand.ExecuteReader()
Return myData
Catch myerror As MySqlException
clsError.logMessage("De server retourneerde de volgende foutmelding: " & myerror.Message)
Return DBNull.Value
End Try
End Function
Why does this conversion error pop-ups? I was sure that 1 or 0 is self-explaining.
Upvotes: 0
Views: 132
Reputation: 164291
Your problem is that you are creating a data reader and returning that. You need to read the integer from the myData
variable before returning it.
So, instead of:
Return myData
You should be able to use:
Return myData.GetInt32(0)
The 0
in that statement identifies the column you want to read, and since you are returning a single column from your SQL query, it is in index 0.
Once you have fixed that, please google 'SQL injection' and 'parameterized SQL queries' before continuing. Having your app open to SQL injection is a serious risk.
Upvotes: 1