VBA data.hasrow while loop (1 Viewer)

sam1fletcher

Registered User.
Local time
Today, 12:26
Joined
Jan 28, 2013
Messages
40
Hey All
Basically i have connect my program to an MySql database

I have a while loop which i think the code is fine but wont work properly.
it basically checks the username and password in if is not correct create a simple MsgBox.

If the username and password is correct then is works also if the username is correct but the password isnt it works. But as soon as the username is wrong nothing happens any clue why??

Code:
 Try
           conn.Open()
            Dim sqlquery As String = "SELECT * FROM NewTable WHERE Username = '" & username.Text & "';"
            Dim data As MySqlDataReader
            Dim adapter As New MySqlDataAdapter
            Dim command As New MySqlCommand
            command.CommandText = sqlquery
            command.Connection = conn
            adapter.SelectCommand = command
            data = command.ExecuteReader
            While data.Read()
                If data.HasRows = True Then
                    If data(2).ToString = password.Text Then
                        PlayerRegistered.Show()
                        ' Me.Close()
                    Else
                        failed.Show()
                    End If
                Else
                    MsgBox("failed pleas try again")
                End If

            End While

        Catch ex As Exception
        End Try

conn is my database pathname

any ideas??
 

spikepl

Eledittingent Beliped
Local time
Today, 13:26
Joined
Nov 3, 2010
Messages
6,142
.net stuff is best asked in .net forums - here we deal with VBA which is far from vb.NET

I reckon the meat of your sub is never executed if the username does not exist in the newtable
 

sam1fletcher

Registered User.
Local time
Today, 12:26
Joined
Jan 28, 2013
Messages
40
Sorry but its literally the loop which is bugging me.

isee what your saying but does the last "else" statement cover that and so should display the MsgBox???
 

spikepl

Eledittingent Beliped
Local time
Today, 13:26
Joined
Nov 3, 2010
Messages
6,142
check if while data.read is ever true - i doubt it, for non-existing users that is. use this opportunity to learn how to step through code using the debugger.. see the path the code takes and inspect the variable values
 

sam1fletcher

Registered User.
Local time
Today, 12:26
Joined
Jan 28, 2013
Messages
40
Great thanks you were rite

no need for a while loop just a couple of statement did the job instead
 

Users who are viewing this thread

Top Bottom